阿里云服务器

我们在做wordpress网站中常常需要调用文章列表,热门文章,最新文章,和随机文章,今天这篇文章给大家分享,通过浏览量来调用文章的方法。

首先在主题函数function.php中加入以下代码:

//网站浏览量统计代码 
function themetuts_record_visitors(){ if (is_singular()) {
 global $post; $post_ID = $post->ID; if($post_ID) { $post_views = (int)get_post_meta($post_ID, 'views', true); if(!update_post_meta($post_ID, 'views', ($post_views+1))) { add_post_meta($post_ID, 'views', 1, true); } } } } add_action('wp_head', 'themetuts_record_visitors'); function themetuts_the_view($before = '', $after = '', $echo = 1) { global $post; $post_ID = $post->ID; $views = (int)get_post_meta($post_ID, 'views', true); if ($echo) echo $before, number_format($views), $after;
 else return $views; }

在文章循环中调用出来:

<?php themetuts_the_view(); ?>

然后在需要调用的位置插入下面代码:

<?php$args=array('meta_key' => 'views','orderby' => 'meta_value','order' => 'date');query_posts($args);while ( have_posts() ) : the_post();?>								
    <li>
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
            <?php if(has_post_thumbnail()) {the_post_thumbnail('post');} else { ?><img src="<?php echo get_template_directory_uri();?>/images/thumbnail/<?php echo rand(1,6);?>.png" alt="<?php the_title(); ?>" /><? } ?>
            <span class="itemtit"><?php the_title(); ?></span>
            <span class="itempoint clr">
                <span class="price">¥<?php the_field('price'); ?></span>
                <span class="hots"><span class="fa fa-sun-o"></span><?php themetuts_the_view(); ?></span>
            </span>
        </a>
    </li><?php endwhile;wp_reset_query();?>

通过上面方法,就可以调用wordpress文章,按照浏览量来排序了。

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