如何在WordPress中为自定义帖子类型和分类法使用相同的slug

4 wordpress

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

我需要注册一个新的自定义帖子类型"教程",因为我想为它们设计一个不同的设计,并将它们与其他帖子分开.我也想按类别组织它们,所以我将使用分类法.我已经阅读了很多教程(这里和谷歌),我发现了许多方法,但结果并不是我所期望的.最后,我在一个插件中编写了这段代码:

<?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
                            )
                     );

}

?>
Run Code Online (Sandbox Code Playgroud)

在管理面板中,现在我可以添加"教程".我还为他们创建了一些类别(photoshop,web等).问题在于slu ..

我现在所拥有的:

  • 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/

但是,如果分类法的重点发生了变化,例如'rewrite' => array( 'slug' => 'tutorials' ),我得到了一个404 error.

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

rgd*_*ign 16

我的答案不能100%解决你的要求,但是,我一直在许多项目中使用这种方式,我认为在这种情况下最好做.

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

在帖子类型args上,chage has_archive到:

'has_archive' => 'tutorials' // This will tell wp to use the taxonomy tutorials for the post type archive
Run Code Online (Sandbox Code Playgroud)

并将重写更改为:

'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,

     ),
Run Code Online (Sandbox Code Playgroud)

在分类法args上,将重写更改为:

'rewrite' => array( 'slug' => 'tutorials', 'with_front' => true, 'hierarchical' => false),
Run Code Online (Sandbox Code Playgroud)

然后你需要运行一次刷新,在init函数的末尾加上这个:

flush_rewrite_rules();
Run Code Online (Sandbox Code Playgroud)

只运行一次,然后从函数中删除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)
Run Code Online (Sandbox Code Playgroud)

注意:正如我所说,与邮政编码档案一样的单一帖子类型slu can不能相同,将同时识别单一和分类档案的slu to将是一个麻烦,因此,我发现最好的是使用这个您可以使用单个帖子的单个子弹和税务档案的复数版本的方法.至少你不会在slug上有像"cat-tutorials"这样的东西,会更好的人类可读性和SEO的好处.

希望有所帮助.