以下是一个完整的WordPress循环标签代码

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
// 获取分类ID为2的分类信息
$category = get_category(2);

if ($category) {
// 获取分类名称
$category_name = $category->name;

// 获取分类链接
$category_link = get_category_link($category->term_id);

// 获取分类文章总数
$category_count = $category->category_count;

// 查询该分类下最新2篇文章(修正数量为2)
$args = array(
'cat' => 2,
'posts_per_page' => 2, // 修复:改为2篇
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);

// 显示分类信息
if ($query->have_posts()) {
echo '<ul class="post-list">';
// 分类信息只显示一次(移到循环外)
echo '<li>
<article class="article-list">
<header class="category-widget-header">
<a href="' . esc_url($category_link) . '"> // 修复:动态链接
<span class="category-title">'
. esc_html($category_name) .
'</span>
<span class="post-count">'
. number_format_i18n($category_count) . // 修复:移除多余点号
'</span>
</a>
</header>';

// 文章循环
while ($query->have_posts()) {
$query->the_post();
?>
<div class="category-latest-article">
<h3 class="entry-title entry-title-small">
<?php if (0 === $query->current_post) : // 仅在第一篇显示Latest ?>
<span class="latest-category-post">Latest : </span>
<?php endif; ?>
<a href="<?php the_permalink(); ?>"> // 修复:移除PHP标签嵌套
<?php the_title(); ?>
</a>
</h3>
</div>
<?php
}

echo '</article>
</li>
</ul>';
} else {
echo '<p>该分类下没有文章</p>';
}

// 重置查询
wp_reset_postdata();
} else {
echo '<p>指定的分类不存在</p>';
}
?>

调用栏目为2的最新文章 7篇,第一篇文章一个循环列表,剩下的一个循环列表

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!-- 栏目2最新文章 -->
<section class="section editors-picks mb-20">
<div class="title-wrap">
<h3 class="section-title">栏目2最新文章</h3>
<a href="<?php echo get_category_link(2); ?>" class="all-posts-url">查看全部</a>
</div>
<div class="row">
<?php
// 获取栏目2的最新文章
$args = array(
'cat' => 2, // 栏目ID为2
'posts_per_page' => 7, // 获取7篇文章(1篇大图 + 6篇小图)
'orderby' => 'date',
'order' => 'DESC'
);

$latest_posts = new WP_Query($args);

if ($latest_posts->have_posts()) {
$count = 0;

// 第一个循环:第一篇文章(大图布局)
while ($latest_posts->have_posts() && $count < 1) {
$latest_posts->the_post();
$count++;
?>
<div class="col-lg-7">
<article class="entry">
<div class="entry__img-holder">
<a href="<?php the_permalink(); ?>">
<div class="thumb-container thumb-75">
<?php if (has_post_thumbnail()) : ?>
<img data-src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'large'); ?>"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php else : ?>
<img data-src="<?php echo get_template_directory_uri(); ?>/static/picture/default.jpg"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php endif; ?>
</div>
</a>
</div>
<div class="entry__body">
<div class="entry__header">
<?php
$categories = get_the_category();
if (!empty($categories)) {
$first_category = $categories[0];
echo '<a href="' . get_category_link($first_category->term_id) . '" class="entry__meta-category">' . esc_html($first_category->name) . '</a>';
}
?>
<h2 class="entry__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<ul class="entry__meta">
<li class="entry__meta-author"><i class="ui-author"></i><a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a></li>
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
<li class="entry__meta-comments"><i class="ui-comments"></i><a href="<?php comments_link(); ?>"><?php comments_number('0', '1', '%'); ?></a></li>
</ul>
</div>
<div class="entry__excerpt">
<p><?php echo wp_trim_words(get_the_excerpt(), 30, '...'); ?></p>
</div>
</div>
</article>
</div>
<?php
}

// 第二个循环:剩余6篇文章(小图列表)
echo '<div class="col-lg-5">';
echo '<ul class="post-list-small">';

while ($latest_posts->have_posts()) {
$latest_posts->the_post();
?>
<li class="post-list-small__item">
<article class="post-list-small__entry">
<div class="post-list-small__img-holder">
<?php if (has_post_thumbnail()) : ?>
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/static/picture/default-thumb.jpg" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php endif; ?>
</div>
<div class="post-list-small__body">
<h3 class="post-list-small__entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<ul class="entry__meta">
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
</ul>
</div>
</article>
</li>
<?php
}
echo '</ul>';
echo '</div>';
} else {
echo '<div class="col-12"><p>栏目2暂无文章</p></div>';
}

wp_reset_postdata();
?>
</div>
</section>
<!-- end 栏目2最新文章 -->

第二个简单的例子

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
// 获取分类ID为2的分类信息
$category = get_category(2);

if ($category) {
// 获取分类名称
$category_name = $category->name;

// 获取分类链接
$category_link = get_category_link($category->term_id);

// 查询该分类下最新2篇文章
$args = array(
'cat' => 2,
'posts_per_page' => 2,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);

// 显示文章列表
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<div class="grid">
<div class="img-holder">
<img src="<?php echo post_thumbnail_src(); ?>" alt="<?php the_title_attribute(); ?>" class="img img-responsive">
<div class="wpo-blog-content">
<div class="thumb"><?php echo esc_html($category_name); ?></div>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>
<ul>
<li><img src="assets/picture/img-17.jpg" alt=""></li>
<li>By <a href="javascript:void(0);">Robert</a></li>
<li><?php the_time('F j, Y'); ?></li>
</ul>
</div>
</div>
</div>
<?php
}
} else {
echo '<p>该分类下没有文章</p>';
}

// 重置查询
wp_reset_postdata();
} else {
echo '<p>指定的分类不存在</p>';
}
?>

调用全站最新文章数量7篇,第一篇一个循环,剩下的一个循环

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<section class="section editors-picks mb-20">
<div class="title-wrap">
<h3 class="section-title">最新文章</h3>
<a href="<?php echo home_url('/latest'); ?>" class="all-posts-url">查看全部</a>
</div>
<div class="row">
<?php
// 获取全站最新文章
$args = array(
'posts_per_page' => 7, // 获取7篇文章(1篇大图 + 6篇小图)
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);

$latest_posts = new WP_Query($args);

if ($latest_posts->have_posts()) {
$count = 0;

// 第一个循环:第一篇文章(大图布局)
while ($latest_posts->have_posts() && $count < 1) {
$latest_posts->the_post();
$count++;
?>
<div class="col-lg-7">
<article class="entry">
<div class="entry__img-holder">
<a href="<?php the_permalink(); ?>">
<div class="thumb-container thumb-75">
<?php if (has_post_thumbnail()) : ?>
<img data-src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'large'); ?>"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php else : ?>
<img data-src="<?php echo get_template_directory_uri(); ?>/static/picture/default.jpg"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php endif; ?>
</div>
</a>
</div>
<div class="entry__body">
<div class="entry__header">
<?php
$categories = get_the_category();
if (!empty($categories)) {
$first_category = $categories[0];
echo '<a href="' . get_category_link($first_category->term_id) . '" class="entry__meta-category">' . esc_html($first_category->name) . '</a>';
}
?>
<h2 class="entry__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<ul class="entry__meta">
<li class="entry__meta-author"><i class="ui-author"></i><a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a></li>
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
<li class="entry__meta-comments"><i class="ui-comments"></i><a href="<?php comments_link(); ?>"><?php comments_number('0', '1', '%'); ?></a></li>
</ul>
</div>
<div class="entry__excerpt">
<p><?php echo wp_trim_words(get_the_excerpt(), 30, '...'); ?></p>
</div>
</div>
</article>
</div>
<?php
}

// 第二个循环:剩余6篇文章(小图列表)
echo '<div class="col-lg-5">';
echo '<ul class="post-list-small">';

while ($latest_posts->have_posts()) {
$latest_posts->the_post();
?>
<li class="post-list-small__item">
<article class="post-list-small__entry">
<div class="post-list-small__img-holder">
<?php if (has_post_thumbnail()) : ?>
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/static/picture/default-thumb.jpg" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php endif; ?>
</div>
<div class="post-list-small__body">
<h3 class="post-list-small__entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<ul class="entry__meta">
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
</ul>
</div>
</article>
</li>
<?php
}
echo '</ul>';
echo '</div>';
} else {
echo '<div class="col-12"><p>暂无文章</p></div>';
}

wp_reset_postdata();
?>
</div>
</section>
<!-- end 全站最新文章 -->