WordPress主题首页index.php模板中,不限制分类循环标签,来调用最新文章数量4个,带分类

风中赏雪 Wordpress Wordpress 2025-09-27 115 0

在WordPress主题的index.php模板中,使用标准循环调用最新4篇文章的代码如下:


<?php
// 创建自定义查询获取最新4篇文章
$args = array(
    'posts_per_page' => 4,  // 显示数量
    'post_status'    => 'publish',  // 只获取已发布文章
    'ignore_sticky_posts' => 1  // 可选:忽略置顶文章
);
$latest_posts = new WP_Query($args);

if ($latest_posts->have_posts()) : 
    while ($latest_posts->have_posts()) : $latest_posts->the_post(); ?>
    
    <li>
        <!-- 文章内容结构 -->
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            <?php the_title(); ?>
        </a>
        <!-- 可选:添加其他元素 -->
        <div class="post-excerpt"><?php the_excerpt(); ?></div>
    </li>
    
    <?php endwhile;
    wp_reset_postdata();  // 重置查询数据
else : ?>
    <li><?php _e('暂无文章', 'your-text-domain'); ?></li>
<?php endif; ?>

关键点说明
posts_per_page:控制显示文章数量

ignore_sticky_posts:避免置顶文章干扰正常排序

wp_reset_postdata():重置主循环数据(必须调用)

模板标签:

the_permalink():文章链接

the_title():文章标题

the_excerpt():文章摘要

the_title_attribute():标题属性(用于title标签)

完整示例(带基本HTML结构)



<ul class="latest-posts-list">
    <?php
    $recent_query = new WP_Query(array(
        'post_type'      => 'post',
        'posts_per_page' => 4,
        'no_found_rows'  => true  // 优化查询性能
    ));
    
    while ($recent_query->have_posts()) : 
        $recent_query->the_post();
        ?>
        <li <?php post_class('post-item'); ?>>
            <article>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <time datetime="<?php echo get_the_date('c'); ?>"><?php echo get_the_date(); ?></time>
                <?php if (has_post_thumbnail()) : ?>
                    <div class="post-thumbnail">
                        <?php the_post_thumbnail('thumbnail'); ?>
                    </div>
                <?php endif; ?>
                <div class="entry-summary">
                    <?php the_excerpt(); ?>
                </div>
            </article>
        </li>
    <?php
    endwhile;
    wp_reset_postdata();
    ?>
</ul>

高级选项

要包含特定分类(示例:ID为3和5的分类):


$args = array(
    'posts_per_page' => 4,
    'cat' => '3,5'  // 包含分类ID
);

排除特定分类(示例:排除ID为7的分类):


$args = array(
    'posts_per_page' => 4,
    'category__not_in' => array(7)  // 排除分类ID
);

下面是带分类的演示


   <div class="post-category">
                     <?php
                        // 获取文章分类
                        $categories = get_the_category();
                        if (!empty($categories)) {

                            $category_links = array();
                            foreach ($categories as $category) {
                                $category_links&#91;&#93; = sprintf(
                                    '<a href="%s" rel="category tag">%s</a>',
                                    esc_url(get_category_link($category->term_id)),
                                    esc_html($category->name)
                                );
                            }
                            echo implode(', ', $category_links);
                        }
                        ?>
</div>