简体   繁体   English

Wordpress 永久链接重写,用于自定义分类法和在 URL 中使用父项和子项层次结构的帖子类型

[英]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.我需要将永久链接设置为如下所示:domain.com/photos/region/country/location 或 domain.com/photos/country/location,具体取决于为每种帖子类型选择的条款。 The region and country terms would be dependent on if the parent terms are Canada , United States , or World .地区和国家术语将取决于父术语是CanadaUnited States还是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自定义帖子类型 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画廊 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/ .现在,此代码在管理面板自定义帖子编辑器中显示以下永久链接结构: domain.com/photos/france/bordeaux/ This post has a parent term of World and a child term of France .这篇文章的父项是World ,子项是France The term France has been set to primary, but making World the primary term has no effect on the permalink. France已设置为主要术语,但将World设为主要术语对永久链接没有影响。 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%/ .我尝试了以下自定义永久链接结构但没有成功: /%category%/%postname%//%postname%/

Any help is greatly appreciated.任何帮助是极大的赞赏。

Cheers,干杯,

Wordpress permalinks are pretty sensitive, do your due diligence before pushing to live. Wordpress 永久链接非常敏感,请在推送之前进行尽职调查。

You don not need to change the permalink in the settings, leave it to /%postname%/ .您无需更改设置中的永久链接,将其保留为/%postname%/

In your function.php , let's add the following just to make that when we mess with the permalinks they actually updates.在您的function.php中,让我们添加以下内容,以便在我们弄乱它们实际更新的永久链接时做到这一点。

/**
* 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 .让我们创建一个名为Photos的自定义帖子类型和一个名为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.我们需要用我们的条款替换我们的自定义分类句柄 %location%。

/**
* 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;  
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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