阿里云服务器

在做wordpress网站中,常常需要调用分类中的置顶文章。

只需做对应模板页面中插入下面代码就可以实现置顶文章的调用:

<?php  
$sticky = get_option('sticky_posts');  
rsort( $sticky );  
$sticky = array_slice( $sticky, 0, 5);  
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );  
if (have_posts()) :  
while (have_posts()) : the_post();  
?>  
这里是需要显示的内容  
<?php endwhile; endif; ?>

具体的样式可以修改,数字也可以调整显示需要置顶文章的数量。

如果是调用热门文章可以用下面的代码:

<?php
$post_num = 10; // 设置调用条数
$args = array(
‘post_password’ => ”,
‘post_status’ => ‘publish’, // 只选公开的文章.
‘post__not_in’ => array($post->ID),//排除当前文章
‘caller_get_posts’ => 1, // 排除置顶文章.
‘orderby’ => ‘comment_count’, // 依评论数排序.
‘posts_per_page’ => $post_num
);
$query_posts = new WP_Query();
$query_posts->query($args);
while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } wp_reset_query();?>


相关阅读:
  • wordpress编辑框不让span过滤的方法
  • wordpress搜索为空时仍然返回结果问题
  • wordpress注册页取消邮箱的方法
  • wordpress用户头像在线调用代码
  • wordpress自动隐藏email注册,直接用户名密码注册
  • wordpress设置游客只能访问指定页面的方法
  • wordpress列表分页显示404页面的解决办法
  • wordpress页面默认排序修改的方法
  • wordpress新版本5.4 “Adderley” 发布上线,更新多项功能
  • wordpress默认编辑器功能增加的方法