wordpress - 如何在 WordPress 中对自定义帖子类型和分类法使用相同的 slugs

标签 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
                            )
                     );

}

?>

在管理面板中,现在我可以添加“教程”。我还为它们创建了一些类别(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' => array( 'slug' => 'tutorials' ),我会收到 404错误

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

最佳答案

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

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

在帖子类型参数上,将 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)

注意:正如我所说,与分类文件slugs相同的单个帖子类型slugs不能相同,对于wp同时识别单个和分类文件slugs来说会很麻烦,所以,我发现的最好的是使用此方法,您对单个帖子使用单数版本,对税务文件使用复数版本。至少你不会在slug上有类似“cat-tutorials”的东西,这样会更容易阅读并且有利于SEO。

希望有帮助。

关于wordpress - 如何在 WordPress 中对自定义帖子类型和分类法使用相同的 slugs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21959851/

相关文章:

php - 在 Woocommerce 中隐藏特定产品的特定运输方式

css - Wordpress - 更新页面后自定义 css 将不起作用

javascript - 我怎样才能从php中获取数据到js

WordPress 创建全宽仪表板小部件

css - 在Divi中调整 map 高度

html - WordPress MySQL选择查询来过滤包含不带src属性或空src的img标签的每个帖子内容

javascript - 根据实际页面有条件地重写 anchor 链接

mysql - MAMP 未连接到本地主机

php - WordPress 随机数存储在哪里?

css - 将 div 定位在另一个 div 之后