小冬SEO

wordpress分类分类、首页、侧边栏优先调用置顶,分类可翻页

2019-02-13 3:18:19 1384 WordPress笔记

在使用wordpress的时候,我们想要在列表页优先展示置顶的内容,但是wordpress列表循环调用标签中,没有优先调用置顶这一项,有的是按照时间、id排序等,就是没有按照置顶排序,下面是实现的方法,一起来看看

1、列表可翻页调用标签

<?php
    $current = $wp_query->query_vars['paged'];
    if($current<=1) {
        query_posts(array('post__in'=>get_option('sticky_posts'),'caller_get_posts'=>1,'cat'=>$cat));
        while(have_posts()) : the_post();
?>
//置顶内容开始
<li>
    <div class="item clearfix">
    <a target="_blank" href="<?php the_permalink() ?>"><img src="<?php echo catch_first_image() ?>"></a>
    <div class="info-item-two">
        <a target="_blank" href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a>
        <p><?php echo get_the_excerpt(); ?>...</p>
    </div>
    </div>
</li>
//置顶内容结束
<?php endwhile; wp_reset_query(); } ?>//置顶循环结束
<!--if 翻页无法指定query查询-->
<?php 
    // query_posts(array('post__not_in'=>get_option('sticky_posts'),'caller_get_posts'=>1));
    if(have_posts()) : while(have_posts()) : the_post();
    if(!is_sticky()){
?>
//非置顶内容继续输出
<li>
    <div class="item clearfix">
    <a target="_blank" href="<?php the_permalink() ?>"><img src="<?php echo catch_first_image() ?>"></a>
    <div class="info-item-two">
        <a target="_blank" href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a>
        <p><?php echo get_the_excerpt(); ?>...</p>
    </div>
    </div>
</li>
<?php }; endwhile;/*endwhile; wp_reset_query();*/ ?>//非置顶循环结束
<div class="pnswitch">
    <?php get_pagenavi();?>//这里放翻页按钮
</div>
<?php endif; ?>

这个教程有个bug,就是在第一个翻页的列表中,置顶的文章是不计算在内翻页条数内的!

2、首页调用置顶内容标签

<?php
    $max = 4;  //请求数量
    $cid = 7;  //请求栏目
    $top = array('cat'=>$cid, 'posts_per_page'=>$max, 'post__in'=>get_option('sticky_posts'), 'caller_get_posts'=>1);
    $num = count(query_posts($top));  //置顶数量
    if($num>=1){
/*** 置顶文章 ***/
        query_posts($top);
        while (have_posts()) : the_post();
?>
           <li class="icom topset">
               <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank">
                   <p><?php the_title(); ?></p>
               </a>
           </li>
<?php 
        endwhile;
        wp_reset_query();
    };
/*** 普通文章 ***/
    $count = $max-$num;  //除去置顶文章外所需加载普通文章数量
    query_posts(array('cat'=>$cid, 'posts_per_page'=>$count, 'post__not_in'=>get_option('sticky_posts'), 'caller_get_posts'=>1));
    while (have_posts()) : the_post();
        if(!is_sticky()){
?>
       <li class="icom">
           <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank">
               <p><?php the_title(); ?></p>
           </a>
       </li>
<?php
        }
    endwhile;
    wp_reset_query();
?>

3、当前分类调用置顶文章,不用翻页

我发现网上那些教程,真的很操蛋,要不就是缺斤少两,要不就是不能用,都没去验证是否能用,就以为的抄过去抄过来,就这个教程找到找不到《wordpress分类页调用当前分类置顶文章,限制数量》,我无非就是想限制一下调用的数量,下面看我总结的方法:

在需要调用置顶文章的分类页面加入如下代码:

<?php 

query_posts(array(

"category__in" => array(get_query_var("cat")), 

"post__in" => get_option("sticky_posts"),

'showposts' => 3,

)

);

while(have_posts()) : the_post(); 

?>  

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" target="_blank">

    <img src="<?php echo catch_first_image() ?>"><b><?php the_title(); ?></b>

</a> 

<?php 

endwhile;

wp_reset_query();

?>

解释一下:

showposts这个是限制数量的

<a>标签里面你自己自定义html

这样就实现了当前分类调用当前分类置顶文章,还能限制调用数量了!

版权保护: 本文由小冬SEO编辑发布,转载请保留链接: http://www.myseoyh.cn/shuo/118.html