WordPress文章按浏览量排序的方法
今天追格小程序小编给大家分享WordPress文章按浏览量排序的方法,适用于调用分类的热门文章,本文仅适合具备一定编程基础的人进行二次开发。
1、展示浏览量的示例代码
在WordPress主题的functions.php文件至下方的“?>”前,加入以下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; }
2、文章页面调用本标签
<?php themetuts_the_view(); ?>
3、按浏览量排序示例代码
<?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插件:Branda 自定义网站仪表盘插件(白标插件)Branda是一个自定义网站仪表板WordPress插件,有些人也叫白标插件。Branda支持自定义网站仪表板的很多位置,支持修改或删除工具栏中的徽标、添加工具栏链接、删除工具栏链接等。
-
支付宝小程序INVALID_PARAMETER(订单数据同步报错)支付宝小程序订单中心提示 INVALID_PARAMETER(参数有误,不同情况下的 sub_msg 可能不同)。
-
支付宝小程序搜索关键词提示 BIZ_ERROR(没有操作权限)支付宝小程序搜索关键词提示 BIZ_ERROR(没有操作权限),错误原因可能是搜索服务工单的 appId,其所属的商家并未授权给当前操作用户
-
WordPress插件:LoginPress注册登录页美化插件LoginPress是一个WordPress登录页美化插件,LoginPress插件在WordPress官网显示有20万+的安装量,该插件和其他登录美化页插件差不多,均可可视化修改登录页面和登录表单,自定义登录页Logo,自定义登录页面背景,自定义登录页面表单样式等。
-
微信小程序移除插屏广告关闭事件的监听函数InterstitialAd.offCloseInterstitialAd.offClose是微信小程序移除插屏广告关闭事件的监听函数。onClose 传入的监听函数。不传此参数则移除所有监听函数。
-
微信小程序wx.getExtConfig获取第三方平台自定义的数据字段wx.getExtConfig是微信小程序获取第三方平台自定义的数据字段函数。
暂无评论,抢个沙发...