zoukankan      html  css  js  c++  java
  • wordpress调用置顶文章sticky_posts的三种方法

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

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

    <?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'规定的值,会用最新文章替补完整。

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

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

    <?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()判断

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

  • 相关阅读:
    迭代器在LinkedList上的删除
    java多线程:CopyOnWriteArrayList
    vs中代码编译通过,但还是有红色波浪线
    vs中项目属性配置
    TortoiseGit安装与配置
    DC(device context)
    weak_ptr 使用
    C++ 中shared_ptr循环引用计数问题
    for_each与lambda表达式联合使用
    new 和 make_shared 在内存上的区别
  • 原文地址:https://www.cnblogs.com/ytkah/p/11576656.html
Copyright © 2011-2022 走看看