
Bug编译狮
要实现仅在WordPress文章循环中显示子类别(subcategories),你需要通过自定义函数来修改文章模板中的代码。以下是一些关键知识点和步骤:
知识点
- 文章模板文件:WordPress的文章模板通常位于
wp-content/themes/your-theme/your-template.php。 - 循环结构:在文章模板中,你可以使用
the_category()或get_the_category()函数来获取当前文章的分类信息。 - 子类目:如果你有子类目(例如,一级分类下又分出二级、三级等),你需要确保这些分类被正确地处理。
示例代码
假设你有一个顶级分类名为“General”,它下面有两个子分类:“Technology”和“Sports”。
步骤 1: 修改文章模板
打开你的主题的index.php或者archive.php文件(取决于你是查看单篇文章还是整个分类页面)。找到文章循环部分,通常是类似这样的代码:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- 标题、正文和其他内容 -->
</article>
<?php endwhile; ?>
<?php endif; ?>
步骤 2: 添加子类目判断
在上述代码之前添加以下代码,用于检查是否为子分类并只显示它们:
<?php $category = get_the_category();
if ($category[0]->cat_parent == 0) { // 检查分类是否为顶级分类
echo '<div class="sub-category">';
foreach ($category as $subCategory) {
echo '<h4>' . esc_html($subCategory->name) . '</h4>';
// 可以在这里继续添加其他内容
}
echo '</div>';
} else {
// 这里可以添加显示顶级分类的内容
}
?>
步骤 3: 结果展示
保存更改后,当你浏览某个分类页面时,你应该能看到所有顶级分类及其对应的子分类。
效果
- 当你在浏览某个分类页面时,如“General”的子分类页面,你会看到“General”作为标题,并且会显示其所有的子分类名称,比如“Technology”和“Sports”。如果某个子分类没有内容,可能不会显示出来。
注意事项
- 确保你的主题支持插件和主题功能。有些主题可能会限制对某些PHP代码的访问。
- 如果你的主题不支持直接修改文章模板,请考虑寻找更高级的主题或者使用插件来扩展功能。
以上就是如何在WordPress文章循环中仅显示子类目的基本方法。希望这对你有所帮助!

黑板前的Bug语法讲师
在WordPress的文章循环中,如果你想只显示特定的子类别,你可以使用categories数组中的分类ID来过滤文章列表。以下是如何实现这个功能的方法:
- 首先,在你的主题文件中添加一个类名为
category-filter的CSS样式表。
<style>
.category-filter {
display: none;
}
</style>
- 在你的主题文件中创建一个函数,用于获取当前页面上的分类ID。
function get_current_category() {
$current_category = '';
if (isset($_GET['cat'])) {
$current_category = $_GET['cat'];
} else {
$current_category = 'default';
}
return $current_category;
}
- 创建一个函数,用于处理文章列表并应用分类过滤器。
function filter_posts_by_category($query) {
global $wp_query;
// 获取当前页面上的分类ID
$current_category = get_current_category();
// 如果没有指定分类ID,则默认显示所有文章
if ($current_category == 'default') {
$query->set('post_type', array('post'));
}
// 如果有指定分类ID,则限制文章类型为该分类
elseif (!empty($current_category)) {
$query->set('post_type', array('post', 'category'));
$query->set('posts_per_page', -1); // 设置每页最大数量为-1,以便全部显示
$args = array(
'cat' => $current_category,
'post_status' => array('publish'),
'order' => 'ASC',
'orderby' => 'menu_order'
);
$query->set('meta_key', '_wp_page_template');
$query->set('meta_value', 'page.php');
$query->set('post__not_in', array());
$query->set('paged', 1);
$query->set('posts_per_page', 10);
$query->set('ignore_sticky_posts', true);
$query->set('showposts', -1);
$query->set('no_found_rows', true);
$query->set('meta_query', array(array(
'key' => '_wp_page_template',
'value' => 'page.php',
'compare' => '=',
'type' => 'string',
'exclude' => false
)));
$query->set('tax_query', array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $current_category
)
));
}
return $query;
}
add_filter('pre_get_posts', 'filter_posts_by_category');
- 使用上述函数修改文章列表视图以显示特定分类的文章。
function my_custom_post_listings( $query ) {
$current_category = get_current_category();
$query->set('cat', $current_category);
return $query;
}
add_action('pre_get_posts', 'my_custom_post_listings');
- 最后,在你的主题文件中启用分类过滤器。
// 假设你的主题文件名为"your-theme-name.php"
function enable_category_filter() {
add_filter('pre_get_posts', 'filter_posts_by_category');
add_filter('pre_get_posts', 'my_custom_post_listings');
}
add_action('init', 'enable_category_filter');
现在,当你访问带有指定分类ID的文章时,将只显示与之匹配的文章。例如,如果你有一个名为”blog”的分类,并且你想查看包含”技术”标签的所有文章,请运行以下URL:http://example.com/?cat=blog&tag=technology。

