wordpress置顶文章按需调用:全文输出或列表输出

  wordpress置顶文章的设置在发布文章时有选项,这个很简单,不懂自己google之,最难的方法是如何在模板里把置顶文章按一定格式输出,这就是本篇文章我们要讨论的内容。

wordpress置顶文章重点函数

  关于置顶文章wordpress有两个常用的函数

  1. is_sticky():判断文章是否是置顶的,是就返回true,不是就返回false
  2. get_option('sticky_posts'): 获取置顶文章ID,返回包含各置顶文章ID的数组

  对于这两个函数怎么使用下面给出两个具体例子

置顶文章例子1:

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

  方法简介:在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; ?> 

置顶文章例子2:

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

  方法简介:通过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(); ?>"><imgsrc="<?php echo catch_that_image()?>" alt="<?php the_title(); ?>"/></a>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li> 
<?php    endwhile; endif; ?> 
</ul> 

注:红色的部分是调用的博客文章里面的图片,如果没有图片则会显示一个系统默认的图片。

<?php
// 获取置顶文章代码
$sticky = get_option( 'sticky_posts' ); //获得所有置顶文章的id
$args = array(
    'numberposts' => 6, // 最多获取6篇置顶文章
    'post__in'  => $sticky
);
$postQuery = get_posts($args);
//循环输出置顶文章
foreach( $postQuery as $post ) : setup_postdata($post);
    ?>
    <p><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( 'echo=0' ); ?>" rel="bookmark"><?php the_title(); ?></a></p>
    <?php
    if ( has_post_thumbnail() ) {
        the_post_thumbnail();
    }
endforeach;
?>

在foreach循环中可以使用所有post相关的模板标签获取信息,例如

  • 标题——the_title()
  • 固定链接—— the_permalink()
  • 特色图像——the_post_thumbnail()

主题开发过程中如果需要在文章页显示当前文章分类的置顶文章可参考如下代码:

<dl>
	<dt><span>置顶</span>推荐</dt>
	<?php
	wp_reset_query();  //重置搜索
	$category = get_the_category();  //读取当前页面分类信息
	query_posts('cat=' . $category[0]->cat_ID);  //查询指定分类文章
	if (have_posts()) : ?>
	<?php while (have_posts()) : the_post();  
		if (is_sticky()): 
		//输出置顶文章
	?>
	<dd>
		<p class="rList_title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
		<p class="rList_img"><?php the_post_thumbnail(); ?></p>
		<span class="rList_tag"></span></dd>
	<?php else: 
			//非置顶文章
	?> 
	<?php endif; ?> 
	<?php endwhile; ?> 
<?php else: ?> 
<dd class="nothing">暂无文章...</dd> 
<?php endif; ?>
 </dl>

将以上代码放置于你的页面模板single.php即可。

返回第一篇置顶文章

    $sticky=get_option('sticky_posts') ;  
    query_posts('p=' . $sticky[0]);  

 $args = array(  
'posts_per_page' => 1,  
'post__in' => get_option('sticky_posts'),  
'caller_get_posts' => 1  
);  
query_posts($args);
注意:第二种方法只能返回最新发表的置顶文章;若当前无置顶文章,返回最新发表文章。
返回第一篇置顶文章;若无,则不返回任何内容

    $sticky = get_option('sticky_posts');  
    $args = array(  
    'posts_per_page' => 1,  
    'post__in' => $sticky,  
    'caller_get_posts' => 1  
    );  
    query_posts($args);  
    if($sticky[0]) {  
    // insert here your stuff...  
    }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值