现在位置: 首页 > CMS知识 > 正文

wordpress调用某分类下的置顶文章

发布时间:2022 年 9 月 23 日

本文作者:帮建站

点击次数:472

<div class=”swiper-wrapper”>
<?php
$args = array(
‘post_type’ => ‘post’,
‘showposts’ => 3,
//’orderby’ => ‘rand’,
‘post__in’ => get_option(‘sticky_posts’),//获取置顶文章
‘cat’ => array(6),//指定分类id
);
?>
<?php
$catquery = new WP_Query($args);
while($catquery->have_posts()) : $catquery->the_post();

?>

<li><a href=”<?php the_permalink(); ?>”> <img src=”<?php echo get_the_post_thumbnail_url(get_the_ID());?>”> <span><?php the_title(); ?></span> </a> </li>
<?php endwhile; ?>

</div>

代码解释:

get_option(‘sticky_posts’) 能调出数据库表 wp_options 下字段 option_name 为 sticky_posts 中保存的所有的置顶文章信息;

再使用 query_posts() 并指定分类 cat 及需要调用的文章数,来输出置顶文章信息,query_posts() 传送门

问题本身还是很简单的,只是对于不熟悉的网友来说不知道什么原因,如果英语不太好的话,就以为自己买了个假主题了。问题本身还是很简单的,只是对于不熟悉的网友来说不知道什么原因,如果英语不太好的话,就以为自己买了个假主题了。

标签: ,

首页 在线 手机