好吧,这取决于你所说的前5条帖子是什么意思。你可以获得阅读量最高、评论最多、最受欢迎的评论等。对于评论,你可以使用参数进行查询
orderby=comment_count
如果希望查看最多,首先需要添加视图计数器,如下所示:
/********* Post Views counter ***********/
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0);
if(!function_exists(\'mytheme_setPostViews\')){
function mytheme_setPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
}
if (!function_exists(\'mytheme_getPostViews\')) {
function mytheme_getPostViews($postID){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
return "0";
}
return $count;
}
}
在你的
single.php
您将在循环乞讨之后添加
<?php if (have_posts()) : while (have_posts()) : the_post();
mytheme_setPostViews(get_the_ID());
使计数器计数。
然后,要拉出浏览量最大的帖子,请创建一个短代码,如
<?php
// Usage: [most_read_articles title="" post_no="" class=""]
function mytheme_most_read_posts( $atts, $content ){
extract(shortcode_atts(array(
\'title\' => \'\',
\'post_no\' => \'\',
\'class\' => \'\',
), $atts));
$pop_arg = array(
\'posts_per_page\' => $post_no,
\'post_type\' => \'post\',
\'meta_key\' => \'post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
)
?>
<div class="most_read <?php echo $class; ?>">
<div class="most_read_title"><?php echo $title; ?></div>
<?php
$popularpost = new WP_Query( $pop_arg );
$i = 1;
if( $popularpost->have_posts() ) :
while ( $popularpost->have_posts() ) :
$popularpost->the_post();
?>
<div class="most_read_post">
<div class="most_read_post_info">
<span class="view_rank"><?php echo $i.\'. \'; ?></span><a href="<?php the_permalink(get_the_ID()); ?>" class="most_read_post_title"><?php echo get_the_title(get_the_ID()); ?></a><span class="post_views">(<?php echo mytheme_getPostViews(get_the_ID()); ?>)</span>
</div>
</div>
<?php $i++;
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
<?php
}
add_shortcode( \'most_read_articles\', \'mytheme_most_read_posts\');
您可以按自己喜欢的方式修改ofc。但方法是一样的。