You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅显示特定分类(certain-category)的文章并控制代码执行?

解决方案:仅在指定分类下执行文章渲染代码

使用WordPress内置的in_category()函数即可实现需求,该函数能判断当前文章是否属于指定分类(支持分类ID、别名或名称)。将你提供的文章渲染代码整体包裹在这个判断语句内,只有当文章属于certain-category分类时,代码才会执行。

修改后的完整代码如下:

<?php if (in_category('certain-category')) : ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>

    <?php if ( has_post_thumbnail() ) : ?>
            <div class="entry-thumb">
                <a href="<?php the_permalink(); ?>" class="H1-posts"  title="<?php the_title(); ?>" >
                    <?php the_post_thumbnail('moesia-thumb'); ?>
                </a>
            </div>
        <?php endif; ?>


        <?php if (has_post_thumbnail()) : ?>
            <?php $has_thumb = ""; ?>
        <?php else : ?>
            <?php $has_thumb = ""; ?>
        <?php endif; ?>

        <div class="post-content <?php echo $has_thumb; ?>">
            <header class="entry-header">
                <?php the_title( sprintf( '<h1 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

                <?php if ( 'post' == get_post_type() ) : ?>
                
                <?php endif; ?>
            </header><!-- .entry-header -->

            <div class="entry-summary">
                <?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
                    <?php the_content(); ?>
                <?php else : ?>
                    <?php the_excerpt(); ?>
                <?php endif; ?>
            </div><!-- .entry-content -->
</article>
<?php endif; ?>

注:你代码中$has_thumb变量无论是否存在缩略图都赋值为空字符串,这部分逻辑可根据实际需求简化调整,不影响分类判断的核心功能。

内容的提问来源于stack exchange,提问作者Bet Co

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 10:15:27