前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >wordpress调用置顶文章sticky_posts的三种方法

wordpress调用置顶文章sticky_posts的三种方法

作者头像
ytkah
发布2019-09-25 14:02:05
8890
发布2019-09-25 14:02:05
举报
文章被收录于专栏:ytkahytkah

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

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

代码语言:javascript
复制
<?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'规定的值,会用最新文章替补完整。

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

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

代码语言:javascript
复制
<?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()判断

代码语言:javascript
复制
<?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/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
网站建设
网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档