参考链接了解您的知识Template Hiearchy 有关WordPress选择模板的详细信息
对于分类法中的分类术语slug(“运动”您的示例),WordPress将尝试使用以下模板(按此顺序)
taxonomy-{taxonomy}-{slug}.php
taxonomy-{taxonomy}.php
taxonomy.php
archive.php
index.php
修改下面的texonomy代码段:
function my_post_type() {
$args = array(
\'labels\' => array(
\'name\' => \'News\',
\'singular_name\' => \'News\',
\'add_new\' => \'Add New News\',
\'add_new_item\' => \'Add New News\',
\'edit_item\' => \'Edit News\',
\'new_item\' => \'New News\',
\'all_items\' => \'All News\',
\'view_item\' => \'View News\',
\'search_items\' => \'Search News\',
\'not_found\' => \'No News Found\',
\'not_found_in_trash\' => \'No News found in Trash\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'News\',
),
\'public\' => true,
\'has_archive\' => true,
\'menu_icon\' => \'dashicons-admin-site-alt3\',
\'supports\' => array(\'title\',\'editor\',\'thumbnail\'),
\'capability_type\' => \'post\',
);
register_post_type(\'news\', $args);
}
add_action(\'init\', \'my_post_type\');
function my_taxonomy() {
$args = array(
\'labels\' => array(
\'name\' => \'News Category\',
\'singular_name\' => \'News Category\',
),
\'public\' => true,
\'hierarchical\' => true,
);
register_taxonomy(\'news_cat\', array(\'news\'),$args);
}
add_action(\'init\', \'my_taxonomy\');
对于您的“体育”、“游戏”、“政治”分类术语页面,WordPress将使用
将此文件放在主题根目录中
taxonomy-news_cat-sports.php
taxonomy-news_cat-games.php
taxonomy-news_cat-politics.php
Permalinks您还指定了url重写,因此假设重写规则已刷新并且没有冲突,以下操作也应该可以
www.example.com/news_cat/games
注意:不要忘记更新永久链接,否则类别术语页面将重定向到404页面
taxonomy-news\\u cat-sports。php
<?php
get_header();
?>
<div id="content-sidebar-wrap">
<div class="wrap">
<main class="content">
<?php
$case_cat_slug = get_queried_object()->slug;
$case_cat_name = get_queried_object()->name;
?>
<h2><?php echo $case_cat_name; ?></h2>
<?php
$al_tax_post_args = array(
\'post_type\' => \'news\', // Your Post type Name that You Registered
\'posts_per_page\' => 999,
\'order\' => \'ASC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'news_cat\',
\'field\' => \'slug\',
\'terms\' => $case_cat_slug
)
)
);
$al_tax_post_qry = new WP_Query($al_tax_post_args);
if($al_tax_post_qry->have_posts()) :
while($al_tax_post_qry->have_posts()) :
$al_tax_post_qry->the_post();
echo \'<div class="post-excerpt">\';
?>
<h2 class="entry-title" itemprop="headline"><a href="<?php the_permalink(); ?>" class="entry-title-link"><?php the_title(); ?></a></h2>
<div class="entry-content"> <?php the_title(); ?> </div>
</div>
<?php
endwhile;
endif;
?>
</main>
</div>
</div>
<?php
get_footer(); ?>