通常在 wordpress 主题开发中,集成了个人中心功能,需要获取文章、视频、下载、专题和商城的文章数量,这个是比较简单,使用 wp_count_posts()函数即可。如果要获取不同用户、不同自定义字段下的不同文章类型的文章数量怎么办,同样很好解决,使用 get_posts()函数。
一、代码
/*不同用户,不同文章类型,不同字段下的文章数量*/
function salong_author_post_field_count($post_type,$user_id,$field) {
global $post;
$post_args = get_posts( array(
'posts_per_page' => -1,
'post_type' => $post_type,
'post_status' => 'publish',
'author' => $user_id
) );
$counter = 0;
foreach ( $post_args as $post ){
$views = absint( get_post_meta( $post->ID, $field, true ) );
$counter += $views;
}
return $counter;
}
二、调用
调用很方便,比如要调用 ID 为 1 的用户,视频(video)类型文章下的有浏览量(views)的文章数量:
echo salong_author_post_field_count('video','1','views');
服务范围 | 1、专业提供WordPress主题、插件汉化、优化、PHP环境配置等服务请详询在线客服 | |||
2、本站承接 WordPress、DedeCMS、ThinkPHP 等系统建站、仿站、开发、定制等服务 | ||||
3、英文模板(主题)安装费用为120元/次,汉化主题首次免费安装(二次安装30元/次) | ||||
售后时间 | 周一至周五(法定节假日除外) 10:00-22:00 | |||
免责声明 | 本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: [email protected]),我们会及时删除,给您带来的不便,我们深表歉意! |
.demo tbody td a strong{color: #666;}.button_nowbuy,.button_demo {margin: 0em auto;border-style: none;border-width: 0px;padding: 0px;}#qrcode_en,#qrcode_cn{width: 130px;left: 10%;bottom: 250%;}a.button_demo:hover + #qrcode_en{visibility:visible;opacity: 1;bottom: 180%;-webkit-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;transition: all .4s ease-in-out;}a.button_demo:hover + #qrcode_cn{visibility:visible;opacity: 1;bottom: 180%;-webkit-transition: all .4s ease-in-out;-o-transition: all .4s ease-in-out;transition: all .4s ease-in-out;}.table.demo{display: none;}
暂无评论内容