wordpress 自定义固定链接结构:/%custom-post-type%/%custom-taxonomy%/%post-name%/

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23698827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:32:13  来源:igfitidea点击:

Custom permalink structure: /%custom-post-type%/%custom-taxonomy%/%post-name%/

wordpresspermalinks

提问by Bruno Cloutier

I'm trying to create a custom permalink structure that will allow me to accomplish the following.

我正在尝试创建一个自定义的永久链接结构,它将允许我完成以下操作。

  1. I have a custom post type called "projects"
  2. I have a custom taxonomy called "project-category" that is assigned to the CPT "projects"
  1. 我有一个名为“项目”的自定义帖子类型
  2. 我有一个名为“项目类别”的自定义分类法,它被分配给 CPT“项目”

I want my permalink structure to look like this:

我希望我的永久链接结构如下所示:

projects/category/project-name

项目/类别/项目名称

or

或者

/%custom-post-type%/%custom-taxonomy%/%post-name%/

/%custom-post-type%/%custom-taxonomy%/%post-name%/

I've been able to succesfully use /%category%/ in permalinks for normal, out-of-the-box WP posts, but not for CPTs.

我已经能够成功地将 /%category%/ 用于普通的、开箱即用的 WP 帖子的永久链接,但不适用于 CPT。

How would creating such a permalink structure affect the URLs or other pages? Is it possible de define a custom permalink structure and restrict it to a single CPT?

创建这样的永久链接结构将如何影响 URL 或其他页面?是否可以定义自定义永久链接结构并将其限制为单个 CPT?

Thanks

谢谢

回答by Steve Grunwell

Lucky for you, I justhad to do this for a client project. I used this answer on the WordPress Stackexchangeas a guide:

幸运的是,我需要为客户项目执行此操作。我在 WordPress Stackexchange 上使用了这个答案作为指导:

/**
 * Tell WordPress how to interpret our project URL structure
 *
 * @param array $rules Existing rewrite rules
 * @return array
 */
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new['projects/([^/]+)/(.+)/?$'] = 'index.php?cpt_project=$matches[2]';
  $new['projects/(.+)/?$'] = 'index.php?cpt_project_category=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

/**
 * Handle the '%project_category%' URL placeholder
 *
 * @param str $link The link to the post
 * @param WP_Post object $post The post object
 * @return str
 */
function so23698827_filter_post_type_link( $link, $post ) {
  if ( $post->post_type == 'cpt_project' ) {
    if ( $cats = get_the_terms( $post->ID, 'cpt_project_category' ) ) {
      $link = str_replace( '%project_category%', current( $cats )->slug, $link );
    }
  }
  return $link;
}
add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );

When registering the custom post type and taxonomy, be sure to use the following settings:

注册自定义帖子类型和分类时,请务必使用以下设置:

// Used for registering cpt_project custom post type
$post_type_args = array(
  'rewrite' => array(
    'slug' => 'projects/%project_category%',
    'with_front' => true
  )
);

// Some of the args being passed to register_taxonomy() for 'cpt_project_category'
$taxonomy_args = array(
  'rewrite' => array(
    'slug' => 'projects',
    'with_front' => true
  )
);

Of course, be sure to flush rewrite rules when you're done. Good luck!

当然,完成后一定要刷新重写规则。祝你好运!

回答by Rohit Kaushik

While you registering your custom post type used slug as

当您注册自定义帖子类型时,使用 slug 作为

$post_type_args = array(
  'rewrite' => array(
    'slug' => 'projects',
    'with_front' => true
  )

You can try with Setting->permalink

您可以尝试使用设置->固定链接

make parent of that post also make your link

使该帖子的父级也使您的链接