【问题标题】:How do I use the same slugs for custom post types and taxonomies in WordPress如何在 WordPress 中为自定义帖子类型和分类法使用相同的 slug
【发布时间】:2014-03-24 11:14:08
【问题描述】:

我是新来的,我需要你的帮助,因为我不明白如何解决这个问题。

我需要注册一个新的自定义帖子类型“教程”,因为我想为它们提供不同的设计,并将它们与其他帖子分开。我还想按类别组织它们,因此我将为此使用分类法。我已经阅读了很多教程(这里和来自谷歌),我找到了很多方法来做到这一点,但结果不是我所期望的。最后,我把这段代码写在一个插件里:

<?php
   /*
   Plugin Name: My Custom Post Types
   Description: A plugin for our custom post types like tutorials etc.
   */

/*====================================================
Register new custom post type - tutorials
======================================================*/
function register_my_custom_post_type_tutorials() {
  $labels = array(
    'name'               => 'Tutorials',
    'singular_name'      => 'Tutorial',
    'add_new'            => 'Add New',
    'add_new_item'       => 'Add New Tutorial',
    'edit_item'          => 'Edit Tutorial',
    'new_item'           => 'New Tutorial',
    'all_items'          => 'All Tutorials',
    'view_item'          => 'View Tutorial',
    'search_items'       => 'Search Tutorials',
    'not_found'          => 'No tutorials found',
    'not_found_in_trash' => 'No tutorials found in Trash',
    'parent_item_colon'  => '',
    'menu_name'          => 'Tutorials'
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'tutorials' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','custom-fields','page-attributes','post-formats' )
  );

  register_post_type( 'wizz-tutorials', $args );
}
add_action( 'init', 'register_my_custom_post_type_tutorials' );

/*====================================================
Register Custom Taxonomies for Tutorials - Categories
======================================================*/

add_action('init', 'register_tutorials_taxonomy');

function register_tutorials_taxonomy() {
// Add new taxonomy, make it hierarchical (like categories)
  register_taxonomy('wizz-tutorials-category',
                    'wizz-tutorials',
                     array (
                           'labels' => array (
                                              'name' => 'Tutorials Categories',
                                              'singular_name' => 'Tutorials Categories',
                                              'search_items' => 'Search Tutorials Categories',
                                              'popular_items' => 'Popular Tutorials Categories',
                                              'all_items' => 'All Tutorials Categories',
                                              'parent_item' => 'Parent Tutorials Category',
                                              'parent_item_colon' => 'Parent Tutorials Category:',
                                              'edit_item' => 'Edit Tutorials Category',
                                              'update_item' => 'Update Tutorials Category',
                                              'add_new_item' => 'Add New Tutorials Category',
                                              'new_item_name' => 'New Tutorials Category',
                                            ),
                            'hierarchical'     => true,
                            'show_ui'          => true,
                            'show_tagcloud'    => true,
                            'rewrite'          => array( 'slug' => 'tuts-category' ),
                            'public'           => true
                            )
                     );

}

?>

在管理面板中,现在我可以添加“教程”。我还为它们创建了一些类别(photoshop、web 等)。问题出在蛞蝓上。

我现在拥有的:

  • mywebsite.com/tutorials/ - 所有教程的存档(使用 archive.php)

  • mywebsite.com/tutorials/post-name - 显示教程(使用 single.php)

  • mywebsite.com/tuts-category/photoshop/ - 只显示那些 属于这一类

这是完美的,但我想要一些不同的东西:

  • mywebsite.com/tutorials/

  • mywebsite.com/tutorials/post-name

  • mywebsite.com/tutorials/photoshop/

但如果分类法的 slug 发生变化,例如 'rewrite' =&gt; array( 'slug' =&gt; 'tutorials' ),我会得到一个 404 error

有没有办法做到这一点?还有一个问题,我的代码正确吗?谢谢!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    关键是在自定义帖子类型的重写中使用相同的自定义分类名称。像这样: "rewrite" => array( "slug" => "web-tutorials/%custom-taxonomy-name%" ),

    而您的自定义分类名称为 custom-taxonomy-name

    你可以找到完整的解释和解决方案here

    【讨论】:

      【解决方案2】:

      我的回答不会 100% 解决您的要求,但是,我在许多项目中一直使用这种方式,我认为在这种情况下最好这样做。

      首先备份您的网站。然后将所有代码放入一个 INIT 函数中,这很重要,因为您需要刷新规则,并且最好在注册结束时发生(我在最后描述)

      在帖子类型 args 上,将 has_archive 更改为:

      'has_archive' => 'tutorials' // This will tell wp to use the taxonomy tutorials for the post type archive
      

      并将重写更改为:

      'rewrite' => array(
              'slug' => 'tutorial', // Notice this will be the slug for single tutorial and can´t be the same as the archive slug, but has sence, since it´s ONE tutorial post, not ALL the tutorials, singlular, plural things in other words.
              'with_front' => true,
      
           ),
      

      在分类参数上,将重写更改为:

      'rewrite' => array( 'slug' => 'tutorials', 'with_front' => true, 'hierarchical' => false),
      

      然后你需要运行一次flush,在init函数的最后加上这个:

      flush_rewrite_rules();
      

      只运行一次,然后从函数中删除 flush_rewrite_rules。这只是为了重建东西,所以你不要永远离开它。

      然后转到永久链接并保存。

      每次您对分类法或帖子类型进行更改时,您都需要刷新规则并保存永久链接,否则,您将收到 404 错误。

      这样你将拥有:

      mywebsite.com/tutorials/ (this will use the archive or taxonomy template, ej: taxonomy-tutorials.php)
      
      mywebsite.com/tutorial/post-name (notice what i describe about single slugs, template in use: single-tutorial.php)
      
      mywebsite.com/tutorials/photoshop/ (this will use the archive or taxonomy template as well and also you could have a particular template only for that term)
      

      注意:正如我所说,与分类档案 slug 相同的单个帖子类型 slug 不能相同,wp 同时识别单个和分类档案的 slug 会很麻烦,所以,我发现的最好的是使用这种方法,其中单个帖子有单数 slug,税务档案有复数版本。至少你不会在 slug 上有类似“cat-tutorials”之类的东西,这样会更好地阅读,对 SEO 也很好。

      希望对您有所帮助。

      【讨论】:

      • 你是个天才 :) 非常感谢你的帮助!我会将您的解决方案用于我的自定义帖子类型,但我有几个问题:1)有没有办法将mywebsite.com/tutorial/ 重定向到mywebsite.com/tutorials/? (如果有人使用该链接) 2)如果我这样做,重定向可能会成为 SEO 的问题? 3) 为这种自定义帖子类型添加标签的正确方法是什么? (标签有用吗?或者我可以只使用类别吗?)再次,非常感谢,你帮了我很多。
      • 标签和类别(如 wp 所称)实际上是分类法,因此您可以在发布自定义类型时添加自己的“税”,或者您可以使用“分类法”将其添加到帖子类型 args ' => 数组('标签')。您可以使用 httaccess 进行重定向,也可以在模板上使用 wp_redirect。在您的情况下,您可以为“教程”创建一个模板并使用 wp_redirect:codex.wordpress.org/Function_Reference/wp_redirect
      • 请注意,仅访问 Settings > Permalinks 就足以刷新永久链接规则,因为 options-permalink.php 调用 flush_rewrite_rules()
      • 是的,你是对的,但我多次看到如果我不手动进行刷新,分类和术语 URL 结构的更改不会改变。我不知道为什么要说实话。
      • 如果我添加另一个分类法,这似乎会失败,我该怎么做?
      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 2017-01-31
      • 2017-02-28
      • 2012-05-01
      • 2015-10-31
      • 2013-08-03
      • 2017-03-30
      • 2017-08-26
      相关资源
      最近更新 更多