有时我们在开发wordpress时需要调用置顶文章sticky_posts,怎么调用呢?几种写法,有用到query_post的,有用到WP_Query,也有用到is_sticky(),下面随ytkah一起来看看吧

  第一种调用置顶文章的方法,用到query_post,代码如下

<?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'规定的值,会用最新文章替补完整。

  如果想调用除了置顶文章外的本栏目其余所有文章怎么操作?

<?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,和第一种方法有点类似,代码如下

<?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 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/

最新文章

  1. 搜索引擎广告过滤Chrome插件
  2. 转自:C#中TextBox水印提示的简单实现
  3. sql 字符次数
  4. (转)springAOP解析-2
  5. C Primer Plus(第五版)8
  6. 剑指Offer22 判断数组是否为某二叉搜索树的后序遍历
  7. Windows 7 EXE图标丢失修复方法
  8. setprecision、fixed、showpoint的用法总结(经典!!超经典!!)
  9. 排列熵算法简介及c#实现
  10. Windows下memcache的配置和使用(python)
  11. java中获取字母和数字的组合
  12. kali linux中文输入法
  13. Structs复习 Structs标签
  14. 使用tf.print()打印tensor内容
  15. openh264 api 使用
  16. 两个大数组foreach,找出相同的key数量,所用的时间对比
  17. JDK 12又来了,我学不动了...
  18. bzoj1630 / bzoj2023 [Usaco2005 Nov]Ant Counting 数蚂蚁
  19. PHP7+Nginx的配置与安装教程详解
  20. PHP截取中文字符串不出现?号的解决方法[原创]

热门文章

  1. TP-LINK WR703N OpenWrt 无线配网历程
  2. FRP represents an intersection of two programming paradigms.
  3. React实例------红绿灯
  4. 总结:WPF中模板需要绑定父级别的ViewModel该如何处理
  5. kafka controller脑裂(多个controller)问题
  6. OpenResty下载安装教程
  7. Java自学-异常处理 Throwable
  8. VS 对话框控件的Tab顺序问题
  9. pandas-22 数据去重处理
  10. Android-----使用SoapObject获取服务器数据