找回密码
 免费注册

wordpress置顶文章调用与设置

[复制链接]
admin 发表于 2019-7-8 19:45:03 | 显示全部楼层 |阅读模式
wordpress网站后台允许我们设置一些置顶文章,然后通过调用置顶文章的代码把它调用到自己的网站前台。下面是最常用的wordpress调用置顶文章代码,适用于所有模板使用。
  1. <?php
  2. $sticky = get_option('sticky_posts');
  3. rsort( $sticky );
  4. $sticky = array_slice( $sticky, 0, 5);
  5. query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
  6. if (have_posts()) :while (have_posts()) : the_post();
  7. ?>

  8. <li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>

  9. <?php endwhile; endif; ?>
复制代码

代码解释:

  • rsort( $sticky ); 对置顶文章数组逆向排序,即大ID在前;
  • $sticky = array_slice( $sticky, 0, 5);控制显示置顶文章的数量,输出置顶文章数,请修改5,0不要动,如果需要全部置顶文章输出,可以把这句注释掉;
  • 'post__in' => get_option('sticky_posts')确定了该LOOP调用的是置顶文章列表。
  • 'caller_get_posts'的作用是排除非指定性文章,即除了置顶文章之外,不显示其他的文章。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

QQ|Archiver|手机版|小黑屋|信息共享网

GMT+8, 2024-5-15 12:26 , Processed in 0.079553 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表