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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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; ?>

带分类的演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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();

// 获取文章分类
$categories = get_the_category();
$category_name = '';
$category_link = '';

if (!empty($categories)) {
$first_category = $categories[0]; // 获取第一个分类
$category_name = $first_category->name;
$category_link = get_category_link($first_category->term_id);
}
?>

<li>
<!-- 显示文章分类 -->
<?php if ($category_name) : ?>
<span class="post-category">
<a href="<?php echo $category_link; ?>"><?php echo $category_name; ?></a>
</span>
<?php endif; ?>

<!-- 文章标题 -->
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>

<!-- 可选:添加其他元素 -->
<div class="post-excerpt"><?php the_excerpt(); ?></div>

<!-- 可选:显示更多文章信息 -->
<div class="post-meta">
<span class="post-date"><?php echo get_the_date(); ?></span>
<span class="post-author"><?php the_author(); ?></span>
</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结构)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<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的分类):)

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


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

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

下面是带分类的演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div class="post-category">
<?php
// 获取文章分类
$categories = get_the_category();
if (!empty($categories)) {

$category_links = array();
foreach ($categories as $category) {
$category_links[] = 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>