现在,我将向您介绍创建分类法的过程,以及创建的自定义分类法的特定类别的帖子分页时出现的错误。
Step 1: 注册自定义帖子类型
register_post_type(\'product\', [
\'supports\' => [\'title\', \'editor\', \'meta\', \'excerpt\'],
\'public\' => true,
\'labels\' => [
\'name\' => \'Products\',
\'add_new_item\' => \'Add new product\',
\'edit_item\' => \'Edit product\',
\'all_items\' => \'All Products\',
\'singular_name\' => \'Product\'
],
\'menu_icon\' => \'dashicons-portfolio\'
]);
Step 2: 为注册分类
product
岗位类型:
register_taxonomy(\'product_category\', \'product\', [
\'hierarchical\' => true,
\'label\' => \'Categories\',
\'show_ui\' => true,
\'show_admin_column\' => true,
\'singular_name\' => \'Category\',
\'rewrite\' => array(\'slug\' => \'product_category\'),
\'query_var\' => true
]);
然后我创建了一个
page template 使用名称
shop_categories_page.php 显示创建的分类项目(类别)。我可以通过url查看所有类别:
http://siteurl.com/categories
然后我创建了wordpress分类法模板,用于显示具有文件名的类别项目
taxonomy-product_category.php<类别项目的hr>URL将为
http://siteurl.com/product_category/itemname. 当我根据url中的当前项目对所有帖子分页时
return error not found 404. 分页url将为
http://siteurl.com/product_category/itemname/page/2. 你可以看到我的所有文件
here<人力资源>
How can be solved my problem with pagination?
最合适的回答,由SO网友:Jacob Peattie 整理而成
你不应该这样做:
$searchProductsByCategory = [
\'posts_per_page\' => 1,
\'post_type\' => \'product\',
\'ignore_sticky_posts\' => 1,
\'orderby\' => \'id\',
\'tax_query\' => [
[
\'taxonomy\' => \'product_category\',
\'terms\' => get_queried_object_id()
]
]
];
$foundProducts = new WP_Query($searchProductsByCategory);
的全部要点
taxonomy-product_category.php
当前类别的帖子已经被查询过了。这就是为什么
get_queried_object_id()
已经是正确的类别,为什么
queried
是过去时。
当您要在相关模板中显示当前类别/帖子类型/存档的帖子时template hierarchy 您需要使用主循环:
if ( have_posts() ) :
while ( have_posts() ) : the_post();
endwhile;
endif;
每当您查看最新的帖子、单个帖子、单个页面、分类术语中的帖子、搜索结果或日期存档时,WordPress都会自动查询这些帖子,并在主循环中显示它们。
这是模板层次结构工作的唯一原因。自定义分类法如何回退到index.php
什么时候taxonomy-$taxonomy.php
如果需要不同的查询,则不存在?这是因为它们不是独立的查询,而是主要的查询。