wordpress制作模版时调用置顶文章

2019年12月11日 飞飞 阅读(1.46K)
知识付费主题,优惠促销中:xx-blog主题

我们在制作wordpress主题时,调用置顶文章是必不可少的一项功能,下面说如何在模版中调用置顶文章。

两个重要函数

置顶文章用到的两个重要函数

1、is_sticky() 判断文章是否置顶

2、get_option(‘sticky_posts’): 获取置顶文章ID,包含所有置顶文章ID的数组

用query_post调用置顶文章

上面就是在query_post中调用文章的方法,具体解释一下

‘post__in’ => get_option(‘sticky_posts’),   //在置顶文章中调取文章
‘posts_per_page’ => 5,  //获取五篇置顶文章
‘ignore_sticky_posts’ => 1  //默认值为0,不排除置顶文章

若是想排除置顶文章外的其余文章用 ‘post__not_in’ => get_option(‘sticky_posts’), 这样就可以在调用列表时排除置顶文章

用WP_Query调用置顶文章

和上面的方法有点类似

<?php 
$args = array( 
'posts_per_page' => -1, 
'post__in' => get_option( 'sticky_posts' ) 
); 
$sticky_posts = new WP_Query( $args ); 
while ( $sticky_posts->have_posts() ) : $sticky_posts->the_post();?> 
<li> 
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a> 
</li> 
<?php endwhile; wp_reset_query();?>

如果只显示置顶文章那么用is_sticky()判断即可。

<?php 
$args = array( 
'posts_per_page' => -1, 
'post__in' => get_option( 'sticky_posts' ) 
); 
$sticky_posts = new WP_Query( $args ); 
while ( $sticky_posts->have_posts() ) : $sticky_posts->the_post();
if(is_sticky()){
?> 
<li> 
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a> 
</li> 
<?php } endwhile; wp_reset_query();?>


关注微信公众号『xx主题网

第一时间了解最新网络动态
关注博主不迷路~

未经允许不得转载:xx主题网 » wordpress制作模版时调用置顶文章
分享到:

评论抢沙发

要发表评论,您必须先

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

扫码关注微信公众号

扫描关注xx主题网