WordPress分类归档页面调用本分类置顶文章

知更鸟
知更鸟
知更鸟
站长
2511
文章
0
粉丝
WordPress5,636阅读模式

通常wordpress置顶文章只显示在首页,分类归档页面不显示置顶文章,内容丰富栏目较多的网站会对推荐的文章进行置顶显示,以便访客打开该分类时第一时间能看到管理员推荐的内容,如果分类列表显示网站所有的置顶文章,显然影响用户体验,因此只显示该分类的置顶推荐文章会更加友好。如想在分类归档页面调用本分类置顶文章,可以用本文的方法实现。
WordPress分类归档页面调用本分类置顶文章

将下面的代码添加到主题archive.php或者category.php模板主循环上面:

  1. <?php
  2.     query_posts(array(
  3.         "category__in" => array(get_query_var("cat")),
  4.         "post__in" => get_option("sticky_posts"),
  5.         'showposts' => 3,
  6.         )
  7.     );
  8.     while(have_posts()) : the_post();
  9. ?>
  10. <h1>置顶<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
  11. <?php
  12.     endwhile;
  13.     wp_reset_query();
  14. ?>

 

其中: 'showposts' => 3,  是显示数量。

在正常的文章列表中排除已置顶的文章:

  1. <?php while(have_posts()) : the_post(); ?>
  2.     <?php if(!is_sticky()){?>
  3.     <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
  4.     <?php the_excerpt(); ?>
  5. <?php } endwhile;?>

原文:http://www.boke8.net/wordpress-call-the-posts-of-this-category.html

本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
微信号已复制
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  9  访客  9