wordpress热门文章调用代码
方案一:普通文章+按浏览量
查询逻辑:普通文章 post、按浏览量 post_views_count 从高到低排序,取 5 条,忽略置顶文章
把下面代码放到要显示的位置。
<div class="sidebar-widget">
<h3>🔥 热门会展资讯</h3>
<ul>
<?php
$popular_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'no_found_rows' => true
));
if ($popular_posts->have_posts()) :
while ($popular_posts->have_posts()) : $popular_posts->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date"><?php echo get_the_date('Y/m/d'); ?></span>
</li>
<?php endwhile; wp_reset_postdata(); else : ?>
<li style="color:#7b8ba3;font-size:13px;">暂无热门文章</li>
<?php endif; ?>
</ul>
</div>
方案二:普通文章+按浏览量+ID
把下面代码放到要显示的位置。
<div class="sidebar-widget">
<h3>🔥 热门新闻</h3>
<ul>
<?php
$popular_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'cat' => 6, //新闻分类ID,自行修改
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'no_found_rows' => true
));
if ($popular_posts->have_posts()) :
while ($popular_posts->have_posts()) : $popular_posts->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date"><?php echo get_the_date('Y/m/d'); ?></span>
</li>
<?php endwhile; wp_reset_postdata(); else : ?>
<li style="color:#7b8ba3;font-size:13px;">暂无热门文章</li>
<?php endif; ?>
</ul>
</div>
使用说明
- 获取分类 ID:WordPress 后台 → 文章→分类,鼠标悬停分类名称,浏览器底部链接里
tag_ID=数字 - 把代码里
cat => 数字替换成真实 ID - 两套列表互相独立,会展资讯只会显示会展类文章,热门新闻只显示新闻类,内容不会重复
- 自带
no_found_rows性能优化、显示日期、空数据友好样式
拓展需求(可选)
如果你之后想要支持多个分类同时调取(例如同时读取分类 5、8、9),把 'cat' => 5 换成 'category__in' => array(5,8,9) 即可。
方案二:普通文章+按浏览量+ID+自动获取父分类 + 所有子分类 ID
重点说明
WP_Query 的 category__in 默认不会自动包含子分类,原生参数做不到。 有两种成熟方案:
- PHP 代码自动获取父分类 + 所有子分类 ID(推荐,直接嵌入你的侧边栏)
- 插件实现(不推荐,尽量少装插件)
完整可用代码(自动包含子分类)
需求示例:父分类 ID = 5,自动查询【5 + 5 所有下级子分类】的热门文章
<div class="sidebar-widget">
<h3>🔥 热门会展资讯</h3>
<ul>
<?php
// 父分类ID,改成你自己的
$parent_cat_id = 5;
// 获取父分类以及它所有子分类ID
$cat_ids = get_term_children($parent_cat_id, 'category');
array_push($cat_ids, $parent_cat_id); // 把父分类本身加进去
$popular_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'category__in' => $cat_ids,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'no_found_rows' => true
));
if ($popular_posts->have_posts()) :
while ($popular_posts->have_posts()) : $popular_posts->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date"><?php echo get_the_date('Y/m/d'); ?></span>
</li>
<?php endwhile; wp_reset_postdata(); else : ?>
<li style="color:#7b8ba3;font-size:13px;">暂无热门文章</li>
<?php endif; ?>
</ul>
</div>
如果有多个父分类,并且各自包含子分类
比如需要父分类 5、9,同时读取它们各自的所有子分类:
<div class="sidebar-widget">
<h3>🔥 热门会展资讯</h3>
<ul>
<?php
// 多个父分类
$parent_cats = [5,9];
$cat_ids = [];
foreach ($parent_cats as $pid) {
$childs = get_term_children($pid, 'category');
$cat_ids = array_merge($cat_ids, $childs);
$cat_ids[] = $pid;
}
// 去重防止重复ID
$cat_ids = array_unique($cat_ids);
$popular_posts = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 5,
'category__in' => $cat_ids,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'no_found_rows' => true
));
if ($popular_posts->have_posts()) :
while ($popular_posts->have_posts()) : $popular_posts->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date"><?php echo get_the_date('Y/m/d'); ?></span>
</li>
<?php endwhile; wp_reset_postdata(); else : ?>
<li style="color:#7b8ba3;font-size:13px;">暂无热门文章</li>
<?php endif; ?>
</ul>
</div>
注意事项
get_term_children()只能获取一级子分类- 如果你的分类是三级、四级深层嵌套(孙分类、曾孙分类),上面代码抓取不到更深层级;
- 若存在多级分类,我可以给你写递归函数获取所有层级子分类;
- 修改
$parent_cat_id/$parent_cats里的数字换成你后台真实分类 ID; - 代码直接放进侧边栏模板即可,无需额外修改函数文件。