wordpress调用置顶文章sticky_posts的三种方法
有时我们在开发wordpress时需要调用置顶文章sticky_posts,怎么调用呢?几种写法,有用到query_post的,有用到WP_Query,也有用到is_sticky(),下面随ytkah一起来看看吧
第一种调用置顶文章的方法,用到query_post,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $query_post = array ( 'posts_per_page' => 10, 'post__in' => get_option( 'sticky_posts' ), 'caller_get_posts' => 1 ); query_posts( $query_post ); ?> <?php while (have_posts()):the_post(); ?> <div class = "swiper-slide" > <a href= "<?php the_permalink(); ?>" > <img src= "<?php the_post_thumbnail_url( 'full' ); ?>" alt= "<?php the_title(); ?>" > <div class = "shadow" > <?php the_title(); ?> </div> </a> </div> <?php endwhile ; ?> <?php wp_reset_query(); ?> |
参数用一个数组的形式放在$query_post
中,关键的参数为'post__in' =>get_option('sticky_posts')和'caller_get_posts' => 0。
'post__in' => get_option('sticky_posts')确定了该 LOOP 调用的是置顶文章列表。
'caller_get_posts'的作用是排除非指定性文章,即除了置顶文章之外,不显示其他的文章。
'posts_per_page' => 10,控制文章的数量
不添加的情况下,如果置顶文章条目不足'posts_per_page'规定的值,会用最新文章替补完整。
如果想调用除了置顶文章外的本栏目其余所有文章怎么操作?
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 | <?php $query_post = array ( 'category__in' => array (get_query_var( 'cat' )), //如果是栏目调用,注意这行要加,否则会调用全站所有文章 'posts_per_page' => 5, 'post__not_in' => get_option( 'sticky_posts' ), //排除置顶 'caller_get_posts' => 1 ); query_posts( $query_post ); ?> <?php while (have_posts()):the_post(); ?> <div class = "item wow zoomIn" > <div class = "img-box" > <img src= "<?php the_post_thumbnail_url( 'full' ); ?>" alt= "<?php the_title(); ?>" > </div> <div class = "text" > <div class = "title" > <h3> <?php the_title(); ?> </h3> </div> <div class = "description" > <p> <?php the_excerpt(); ?> </p> </div> <div class = "more" > <a href= "<?php the_permalink(); ?>" >Read More</a> </div> </div> </div> <?php endwhile ; ?> <?php wp_reset_query(); ?> |
第二种写法用到WP_Query,和第一种方法有点类似,代码如下
1 2 3 4 5 6 7 8 9 10 11 | <?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()判断
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 | <?php if (have_posts()) : ?> <p>文章列表如下</p> <ul> <?php while (have_posts()) : the_post(); if (is_sticky()): global $more ; // 设置全局变量$more $more = 1; ?> <li> <h2>[置顶]<a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>" rel= "bookmark" ><?php the_title(); ?></a><h2/> <p><?php the_content(); ?></p> </li> <?php else : global $more ; $more = 0; ?> <li> <h2><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>" rel= "bookmark" ><?php the_title(); ?></a><h2/> <p><?php the_content( '阅读更多' ); ?></p> </li> <?php endif ; ?> <?php endwhile ; ?> </ul> <?php else : ?> <h2>没有找到更多文章</h2> <?php endif ; ?> |
关于置顶文章wordpress有两个常用的函数
is_sticky():判断文章是否是置顶的,是就返回true,不是就返回false
get_option('sticky_posts'): 获取置顶文章ID,返回包含各置顶文章ID的数组
首页展示文章时,如果是置顶文章就全文输出
方法简介:在loop循环时,通过 is_sticky()
判断是否是置顶文章
是的话就设置全局变量$more=1;然后调用 the_content()
;就是全文输出了
否则不是置顶文章的话就设置全局变量 $more=0;然后调用 the_content('更多...');就是截取<--more-->标签后的输出
以上三种方法可以灵活运用,祝大伙开发愉快!
参考资料https://developer.wordpress.org/reference/classes/wp_query/

加微信交流
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2014-09-24 微信内测"微视频" 朋友圈可以发6-8秒短视频