如何在WordPress中为自定义帖子类型和分类使用相同的slug
我是新来的,我需要你的帮助,因为我不知道如何解决这个问题 我需要注册一个新的自定义帖子类型“tutorials”,因为我想为它们提供不同的设计,并将它们与其他帖子分开。我还想对它们进行分类,因此我将使用分类法。我已经阅读了很多教程(在这里和从谷歌),我发现了很多方法来做到这一点,但结果不是我所期望的。最后,我在一个插件中编写了以下代码:如何在WordPress中为自定义帖子类型和分类使用相同的slug,wordpress,Wordpress,我是新来的,我需要你的帮助,因为我不知道如何解决这个问题 我需要注册一个新的自定义帖子类型“tutorials”,因为我想为它们提供不同的设计,并将它们与其他帖子分开。我还想对它们进行分类,因此我将使用分类法。我已经阅读了很多教程(在这里和从谷歌),我发现了很多方法来做到这一点,但结果不是我所期望的。最后,我在一个插件中编写了以下代码: <?php /* Plugin Name: My Custom Post Types Description: A plugin for
<?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/
'rewrite'=>array('slug'=>'tutorials')
,我会得到一个404错误
有办法做到这一点吗?还有一个问题,我的代码正确吗?谢谢 我的回答不能百分之百地解决您的请求,但是,我已经在许多项目中使用了这种方法,我认为在这种情况下最好这样做
首先,备份您的站点。然后将所有代码放入一个INIT函数中,这一点很重要,因为您需要刷新规则,最好在注册结束时使用hapens(我在末尾描述了这一点)
在post类型args上,chage将_归档到:
'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),
在分类法参数上,将“重写”更改为:
'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),
然后,您需要运行一次刷新,在init函数末尾添加以下内容:
flush_rewrite_rules();
只运行一次,然后从函数中删除flush_rewrite_规则。这只是为了重建东西,所以你永远不会离开它
然后转到永久链接并保存
每次对分类法或post类型slug进行更改时,都需要刷新规则并保存永久链接,否则将收到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教程”的东西,会更好地让人可读,对SEO来说也很好
希望有帮助。关键是在重写自定义文章类型时使用相同的自定义分类名称。像这样:
“rewrite”=>数组(“slug”=>“web教程/%自定义分类名称%”,
而您的自定义分类名称是自定义分类名称
您可以找到的完整解释和解决方案您是个天才:)非常感谢您的帮助!我将使用您的解决方案来定制帖子类型,但我有几个问题:1)有没有办法将mywebsite.com/tutorial/
重定向到mywebsite.com/tutorials/
?(如果有人使用该链接)2)如果我这样做,重定向可能会成为SEO的一个问题?3) 如何正确地为这种自定义帖子类型添加标签?(标记有用吗?或者我可以只使用类别吗?)再次感谢您,您帮了我很多。标记和类别(wp称之为)实际上是分类法,因此您可以在自定义类型上添加您自己的“税”,也可以使用“分类法”=>array('tag')将其添加到post类型参数上。您可以使用httaccess重定向,也可以在模板上使用wp_重定向。在你的例子中,你可以为“教程”创建一个模板,然后使用wp\u重定向:注意,仅仅访问设置>永久链接就足以刷新永久链接规则,因为选项permalink.php
调用刷新重写规则()
。是的,你是对的,但是我看到很多次如果我不手动刷新,分类法和术语URL结构的更改不会更改。老实说,我不知道确切的原因。如果我添加另一个分类法,这似乎会失败,我将如何做到这一点?