Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Support %category% tag for 'attachment' post type
- */
- function pm_support_category_tag( $default_uri, $native_slug, $post, $slug, $native_uri ) {
- if ( $native_uri || empty( $post->post_type ) || empty( $post->post_parent ) || $post->post_type !== 'attachment' ) {
- return $default_uri;
- }
- // 1. Get terms assigned to parent post
- $taxonomy = 'category';
- $rel_post = $post->post_parent;
- $terms = wp_get_object_terms( $rel_post, $taxonomy );
- // 2. Sort the terms
- if ( ! empty( $terms ) ) {
- $terms = wp_list_sort( $terms, array(
- 'parent' => 'DESC',
- 'term_id' => 'ASC',
- ) );
- }
- // 3A. Try to use Yoast SEO Primary Term
- $replacement_term = Permalink_Manager_Helper_Functions::get_primary_term( $rel_post, $taxonomy, false );
- // 3B. Get the first assigned term to this taxonomy
- if ( empty( $replacement_term ) ) {
- $replacement_term = ( ! is_wp_error( $terms ) && ! empty( $terms ) && is_object( $terms[0] ) ) ? Permalink_Manager_Helper_Functions::get_lowest_element( $terms[0], $terms ) : '';
- $replacement_term = apply_filters( 'permalink_manager_filter_post_terms', $replacement_term, $rel_post, $terms, $taxonomy, $native_uri );
- }
- $replacement = Permalink_Manager_Helper_Functions::get_term_full_slug( $replacement_term, $terms, 2, $native_uri );
- return str_replace( '%category%', $replacement, $default_uri );
- }
- add_filter( 'permalink_manager_filter_default_post_uri', 'pm_support_category_tag', 10, 5 );
Add Comment
Please, Sign In to add comment