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

wordpress调用置顶文章显示置顶数量的三种方法
http://www.511yj.com/wordpress-query-option.html

在WordPress中,我们有时候要调用置顶的文章,并且关键的是控制显示置顶的文章的数量,你或许试了网上的很多方法,总有不如意的地方,今天一起讨论一下。

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

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

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

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

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

置顶方法二
wordpress置顶文章重点函数

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

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

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

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

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

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

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

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

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

这是网上最多的方法,但在实际测试中,当显示数目修改为大于6时,最多只是6,而后台置顶的文章有13条,可以全部显示,但无法控制数量,希望高手指教。

补充方法
可以通过 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() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; wp_reset_query();?>