wordpress调用置顶文章显示置顶数量的三种方法

作者: admin 分类: wordpress主题制作教程 发布时间: 2018-08-17 01:51

了解query_posts函数

首先,你需要了解query_posts函数。该函数的作用就是对文章进行检索、挑选、排序,在其后的LOOP循环中使用经过挑选、排序的文章。例如:

  1. <?php
  2. query_posts('posts_per_page=10&ignore_sticky_posts=1&orderby=rand');
  3. while(have_posts()):the_post();
  4.  echo '<li>';the_title();echo '</li>';
  5. endwhile;
  6. wp_reset_query();

将随机列出一条文章的标题。

置顶方法一强烈推荐

接下来,我们就是要通过对query_posts的参数进行调整,挑选出置顶的文章列表了。

  1. $query_post = array(
  2.  'posts_per_page' => 10,
  3.  'post__in' => get_option('sticky_posts'),
  4.  'caller_get_posts' => 1
  5. );
  6. query_posts($query_post);
  7. ?>
  8. <ul style="display:none;">
  9. <?php while(have_posts()):the_post(); ?>
  10. <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  11. <?php endwhile; ?>
  12. </ul>
  13. <?php
  14. 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’规定的值,会用最新文章替补完整。

置顶方法二

wordpress置顶文章重点函数

关于置顶文章wordpress有两个常用的函数
is_sticky():判断文章是否是置顶的,是就返回true,不是就返回false
get_option(‘sticky_posts’): 获取置顶文章ID,返回包含各置顶文章ID的数组

首页展示文章时,如果是置顶文章就全文输出

方法简介:在loop循环时,通过 is_sticky()判断是否是置顶文章

是的话就设置全局变量$more=1;然后调用 the_content();就是全文输出了

否则不是置顶文章的话就设置全局变量 $more=0;然后调用 the_content(‘更多…’);就是截取<–more–>标签后的输出

  1. <?php if (have_posts()) : ?>
  2. <p>分章列表如下</p>
  3. <ul>
  4.     <?php while (have_posts()) : the_post();
  5.         if (is_sticky()):
  6.             global $more;    // 设置全局变量$more 
  7.             $more = 1;
  8.     ?>
  9.     <li>
  10.         <h2>[置顶]<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a><h2/>
  11.         <p><?php the_content(); ?></p>
  12.     </li>
  13.     <?php else:
  14.             global $more;
  15.             $more = 0;
  16.     ?>
  17.     <li>
  18.         <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a><h2/>
  19.         <p><?php the_content('阅读更多'); ?></p>
  20.     </li>
  21.     <?php endif; ?>
  22. <?php    endwhile; ?>
  23. </ul>
  24. <?php else: ?>
  25. <h2>没有找到相应文章</h2>
  26. <?php endif; ?>

置顶方法三

一次性把置顶文章全部找出来,然后用列表的方法呈现

方法简介:通过 get_option('sticky_posts')函数把置顶文章id全部找出来,再通过 query_posts() 函数对这部分id的文章循环列表输出

  1. <ul>
  2. <?php
  3.     $sticky = get_option('sticky_posts');
  4.     rsort( $sticky );//对数组逆向排序,即大ID在前 
  5.     $sticky = array_slice( $sticky, 0, 5);//输出置顶文章数,请修改5,0不要动,如果需要全部置顶文章输出,可以把这句注释掉 
  6.     query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
  7.     if (have_posts()) :while (have_posts()) : the_post();
  8. ?>
  9. <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
  10. <?php    endwhile; endif; ?>
  11. </ul>

原文网址:http://www.511yj.com/wordpress-query-option.html

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注

Powered by 草根站长笔记 © 2015-2020 草根站长笔记 Inc.版权所有,禁止转载