現在有比較新的作法,我直接把code貼在內文。
<?php $args = array( 'posts_per_page' => -1, /* get 4 posts, or set -1 for all */ 'orderby' => 'meta_value_num', /* this will look at the meta_key you set below */ 'meta_key' => 'post_views_count', 'order' => 'DESC', 'post_type' => 'post', //'post_type' => array('news','database'), 'post_status' => 'publish' ); $myposts = new WP_Query( $args ); if ($myposts->have_posts()) { while ($myposts->have_posts()) { $myposts->the_post(); ?> <div class="mb-1 small"> <div class="p-1"> <a href="<?php the_permalink() ?>" target='blank'> <span> <?php the_title(); ?> <small><span class=""> <?php echo 'x'; echo getPostViews(get_the_ID()); ?> </span></small> </span> <br> <span class="font-weight-lighter"> <?php $content = get_the_excerpt(); $trimmed_content = mb_substr($content, 0, 87); echo $trimmed_content; ?> </span> </a> </div> </div> <?php } } ?>
以此網站為例,我在以下己個地方加入了最多人瀏覽與計數功能:
以下以首頁為例:
參數說明:11, 17, 表示顯示11篇文章,每篇文章只顯示17字
Mr.M 發佈於 1月27日2011年