如何在 WordPress 中为自定义帖子类型和分类使用相同的 slug [英] How do I use the same slugs for custom post types and taxonomies in WordPress

查看:41
本文介绍了如何在 WordPress 中为自定义帖子类型和分类使用相同的 slug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new here and I need your help because I don't understand how to solve this.

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

I need to register a new custom post type "tutorials", because I want to have a different design for them, and keep them separately from the rest of posts. I also want to organize them in categories, so I will use taxonomies for this. I have read many tutorials (here and from Google) and I found many ways to do this, but the result isn't what I have expected. Finally, I wrote this code in a plugin:

<?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 等).问题出在蛞蝓上.

In the admin panel, now I can add "tutorials". I have also created some categories for them (photoshop, web, etc.). The problem is with the slug.

我目前拥有的:

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

  • mywebsite.com/tutorials/ - archive of all tutorials (using archive.php)

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

mywebsite.com/tutorials/post-name - display the tutorial (using single.php)

mywebsite.com/tuts-category/photoshop/ - 只显示教程属于这一类

mywebsite.com/tuts-category/photoshop/ - displays only tutorials that belong to this category

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

This is perfect, but I want something different:

  • mywebsite.com/tutorials/

mywebsite.com/tutorials/post-name

mywebsite.com/tutorials/photoshop/

但是如果分类法的 slug 发生了变化,例如,'rewrite' =>array( 'slug' => 'tutorials' ),我收到 404 错误.

But if there is a change to the slug for taxonomies, e.g., 'rewrite' => array( 'slug' => 'tutorials' ), I'm getting a 404 error.

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

Is there a way to do this? And another question, is my code correct? Thanks!

推荐答案

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

My answer will not solve 100% your request, but, i have been using this way on many projects and i think is the best to do in this cases.

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

First of all backup your site. Then put all the code into one INIT function, this is important since you need to flush rules and it´s better if hapens at the end of the registration (i describe this at the end)

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

On post type args, chage has_archive to:

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

     ),

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

And on the taxonomy args, change rewrite to:

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

然后你需要运行一次flush,在init函数的末尾添加:

Then you need to run once a flush, at the end of the init function add this:

flush_rewrite_rules();

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

Run just once, then remove the flush_rewrite_rules from the function. This is only for rebuild the things, so you don´t leave that for ever.

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

Then go to permanentlinks and save.

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

Every time you made changes into taxonomies or post types slugs, you will need to flush rules and save permalinks, if not, you will recive a 404 error.

这样你就会:

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 的好处.

NOTE: As i said, single post type slugs same as taxonomy archives slugs can´t be the same, will be a trouble to wp to recognize slugs for single and taxonomy archives at same time, so, the best i found is to use this method where you have a singular slug for single posts and the plural version for tax archives. At least you will not have something like "cat-tutorials" on the slug, will be better human readable and nice for SEO.

希望有所帮助.

这篇关于如何在 WordPress 中为自定义帖子类型和分类使用相同的 slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆