简体   繁体   中英

Wordpress permalink rewrite for custom taxonomy and post type that uses parent and child terms hierarchy in URLs

I have registered a custom taxonomy and post type to handle the display of photo galleries on my website. I am needing to setup the permalinks to look like this: domain.com/photos/region/country/location or domain.com/photos/country/location depending on the selected terms for each post type. The region and country terms would be dependent on if the parent terms are Canada , United States , or World .

An example:

  • Canada (parent term)
    • Alberta (child term)
  • United States (parent term)
    • Arizona (child term)
  • World (parent term)
    • France (child term)

Register taxonomy code

register_taxonomy( 'photo_galleries', array( 'photos' ),
  array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'galleries',
      'with_front' => false
    ),
));

Custom post type arguments

$args = array(
  'supports' => $supports,
  'labels' => $labels,
  'hierarchical' => true,
  'public' => true,
  'rewrite' => array(
    'slug' => 'photos',
    'with_front' => false
  ),
  'menu_position' => 5,
  'menu_icon' => 'dashicons-format-gallery',
  'has_archive' => true,
);
register_post_type( 'photos', $args );

Galleries post_type_link function

function galleries_post_type_link( $url, $post ) {
  if ( $post->post_type == 'photos' ) {
    global $post;
    $terms = get_the_terms( $post->id, 'photo_galleries' );
    $term = $terms[0]->slug;
    $url = str_replace('photos/', 'photos/' . $term . '/', $url);
  }
  return $url;
}
add_filter('post_type_link', 'galleries_post_type_link', 10, 4);

Right now this code is showing the following permalink structure in the admin panel custom post editor: domain.com/photos/france/bordeaux/ . This post has a parent term of World and a child term of France . The term France has been set to primary, but making World the primary term has no effect on the permalink. As you can see, France is only being used in the permalink.

I have tried the following custom permalink structures with no success: /%category%/%postname%/ and /%postname%/ .

Any help is greatly appreciated.

Cheers,

Wordpress permalinks are pretty sensitive, do your due diligence before pushing to live.

You don not need to change the permalink in the settings, leave it to /%postname%/ .

In your function.php , let's add the following just to make that when we mess with the permalinks they actually updates.

/**
* Remove rewrite rules and then recreate rewrite rules.
* @link https://developer.wordpress.org/reference/functions/flush_rewrite_rules/
* Should be removed before pushing to live.
*/
flush_rewrite_rules();
add_action( 'after_switch_theme', 'flush_rewrite_rules' );

Let's create a custom post type called Photos and a custom taxonomies called Locations .

/**
* Register question custom post type.
* @link https://developer.wordpress.org/reference/functions/register_post_type/
*/
add_action( 'init', 'wp_so66575072_custom_post_type' );
function wp_so66575072_custom_post_type() {
    $args = [
        'label' => 'Photos',
        'labels' => [ 
            'singular_name' => 'Photo', 
        ],
        'public' => true,
        'show_in_rest' => true, //Enable Gutenberg
        'menu_position' => -1,
        'menu_icon' => 'dashicons-editor-quote',
        'capability_type' => 'post',
        'taxonomies' => [ 'locations', ],
        'has_archive' => true,
        'delete_with_user' => false,
        'rewrite' => [
            'slug' => 'photos/%locations%',
        ],
    ];
    register_post_type( 'photos', $args );
};

/**
* Register locations custom taxonomy.
* @link https://developer.wordpress.org/reference/functions/register_taxonomy/
*/
add_action( 'init', 'wp_so66575072_custom_taxonomy' );
function wp_so66575072_custom_taxonomy() {
    $args = [
        'labels' => [
            'name' => 'Locations',
            'singular_name' => 'Location' 
        ],
        'hierarchical' => true,
        'show_in_rest' => true, //Enable Gutenberg
        'rewrite' => [
            'slug' => 'locations',
            'hierarchical' => true,
        ],
    ];
    register_taxonomy( 'locations', [ 'photos' ], $args );
};

We need to replace our custom taxonomy handle %location% by our terms.

/**
* Replace custom taxonomy permalink handle.
* @link https://developer.wordpress.org/reference/hooks/post_type_link/
*/
add_filter( 'post_type_link', 'wp_so66575072_post_type_link' );
function wp_so66575072_post_type_link( $post_link ) {
    $taxonomy = 'locations';
    //@link https://developer.wordpress.org/reference/functions/get_the_terms/
    $terms = get_the_terms( get_the_ID(), $taxonomy );
    $slug = [];
    foreach ( $terms as $term ) {
        //@link https://developer.wordpress.org/reference/functions/sanitize_title_with_dashes/
        if ( $term->parent == 0 ) {
            array_unshift( $slug, sanitize_title_with_dashes( $term->name ) );
        } else {
            array_push( $slug, sanitize_title_with_dashes( $term->name ) );
        };
    };
    if ( ! empty( $slug ) ) {
        return str_replace( '%' . $taxonomy . '%' , join( '/', $slug ) , $post_link );
    }
    return $post_link;  
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM