主页/WordPress笔记/博客文章/综合文章/如何在 WordPress 文章循环中仅显示父类别

如何在 WordPress 文章循环中仅显示父类别

Bug编译狮

Bug编译狮

在WordPress中,如果你想要在文章循环中只显示特定类别的文章(例如:父类别),你可以通过修改模板文件来实现这个功能。以下是如何做的一些建议和一个示例。

解释

  1. 找到正确的模板文件: 通常情况下,你需要编辑的模板文件是 single.php 或者 archive.php,具体取决于你是否正在处理单篇文章还是整个分类的文章列表。

  2. 添加条件语句: 在这些模板文件中,你可以在循环开始之前添加一些逻辑判断,以确保只有指定类别下的文章才会被显示。

  3. 示例代码: 假设你想只显示名为 “parent-category” 的父类别下的文章,你可以在 single.php 文件中这样编写:

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <!-- Your post content here -->
    <?php endwhile; ?>
<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<!-- Show only parent category articles -->
<?php $args = array(
    'category_name' => 'parent-category',
);
$categories = get_categories($args); // 获取父类目
if (!empty($categories)) {
    foreach ($categories as $cat) { ?>
        <h2 class="post-cat-title">
            <?php echo $cat->name; ?> - 
            <a href="<?php echo get_category_link($cat->term_id); ?>" title="<?php echo sprintf(__('View all posts in %s', 'textdomain'), $cat->name); ?>"><?php echo $cat->name; ?></a>
        </h2>
        <?php $child_posts = get_children(array('post_parent' => $cat->term_id)); ?>
        <?php if(!empty($child_posts)):?>
            <ul class="children-posts">
                <?php foreach ($child_posts as $child_post): ?>
                    <li><a href="<?php the_permalink($child_post->ID); ?>"><?php the_title('<span>', '</span>'); ?></a></li>
                <?php endforeach; ?>
            </ul>
        <?php endif;?>
    <?php } ?>
<?php endif; ?>
  1. 使用步骤:

    • 将上述代码复制到 single.phparchive.php 文件中。
    • 修改 $args 对象中的 category_name 参数为“parent-category”。
    • 运行 WordPress 更新或重新加载页面,查看结果。
  2. 效果: 确保你已经正确地设置了父类目的名称,比如在数据库中创建了一个名为“parent-category”的类别,并且该类别下确实有文章。

以上就是如何在WordPress文章循环中仅显示父类别的一些基本方法。根据实际情况,你可能还需要调整代码以适应不同的需求,比如添加样式、分页等。

黑板前的Bug语法讲师

黑板前的Bug语法讲师

在WordPress中,要实现只显示指定父类别的文章,可以使用子类和嵌套标签来创建一个特定的分类过滤器。下面是一个简单的例子,展示了如何通过子类名来筛选文章。

步骤 1: 创建一个子类

首先,我们需要创建一个子类,该子类将用于处理文章过滤请求。我们将命名为 only_parent_category

function only_parent_category() {
    $parent_id = get_query_var('cat');
    if (is_category($parent_id)) {
        return true;
    }
}
add_action('template_redirect', 'only_parent_category');

步骤 2: 在主题文件中启用子类

为了使这个功能生效,需要在主题文件中激活子类。这里以一个名为 my_theme.php 的主题文件为例:

function my_theme_setup() {
    add_filter('show_in_rest', '__return_false'); // 避免REST API中的过滤
    add_action('init', 'register_my_categories'); // 注册我们的子类别
}

function register_my_categories() {
    register_taxonomy(
        'parent_cat',
        array('post'),
        array(
            'hierarchical' => false,
            'label'       => 'Parent Category',
            'public'      => true,
            'rewrite'     => array('slug' => 'parent-category-slug'),
        )
    );
}

步骤 3: 编辑文章模板

接下来,在你的文章模板(例如 single.php)中,你可以根据需要显示或隐藏特定的文章。这里展示了一个简单的示例,只显示包含特定父类别的文章:

<?php
get_header(); ?>
<div class="container">
    <?php while (have_posts()) : the_post(); ?>
        <div class="row">
            <?php if (is_category(get_the_ID(), 'parent-cat')) : ?>
                <!-- 只显示包含特定父类别的文章 -->
            <?php endif; ?>
            <?php the_content(); ?>
        </div>
    <?php endwhile; ?>
</div>

<?php get_footer(); ?>

在这个示例中,我们检查当前文章是否属于特定的父类别。如果属于,则显示包含特定父类别的文章;否则,不显示任何内容。

示例代码详解

1. 子类定义

function only_parent_category() {
    $parent_id = get_query_var('cat');
    if (is_category($parent_id)) {
        return true;
    }
}
  • 函数only_parent_category() 是一个自定义过滤器,它接受两个参数:当前的请求参数 $cat 和一个布尔值。
  • 条件判断:根据 $cat 参数,判断当前请求是否为某个特定分类。
  • 返回结果:如果是,返回 true 表示允许执行进一步操作;如果不是,返回 false 停止执行此过滤器。

2. 主题设置

function my_theme_setup() {
    add_filter('show_in_rest', '__return_false'); // 避免REST API中的过滤
    add_action('init', 'register_my_categories'); // 注册我们的子类别
}
  • 注册子类别:在 register_my_categories() 函数中,我们注册了新的分类 parent_cat,其属性包括:
    • hierarchical:否,表示该分类不是树状结构。
    • label:父类别名称。
    • public:是否公开。
    • rewrite:重写规则,用于在 REST API 中提供更好的 URL 结构。

3. 模板编辑

<?php
get_header(); ?>
<div class="container">
    <?php while (have_posts()) : the_post(); ?>
        <div class="row">
            <?php if (is_category(get_the_ID(), 'parent-cat')) : ?>
                <!-- 只显示包含特定父类别的文章 -->
            <?php endif; ?>
            <?php the_content(); ?>
        </div>
    <?php endwhile; ?>
</div>

<?php get_footer(); ?>
  • 文章模板:在文章页面上,我们通过 if 语句检查当前文章是否属于特定的父类别。如果属于,就显示包含该类别的文章;否则,显示默认的内容。

通过以上步骤,您可以有效地在WordPress文章循环中仅显示指定父类别的文章。请确保在实际部署时考虑安全性,特别是在敏感数据如密码和用户信息方面。