从多WP程序站长很喜欢把自己的站点信息展示出来;虽然wordpress的在默认功能里没有这些数量信息,但是我们可以通过强大的wordpress的函数代码调用出来为自己所用
这个功能的调用方法很简单;只需要简单的复制粘贴就可以获取到,
下面列出了所有网站可统计数量信息的调用函数;
操作步骤如下
选择的WordPress主题内的首页(的index.php)或其它页面文件,在相应位置添加函数调用代码
//文章
<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish; ?> 篇
//页面
<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?> 个
//草稿
<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?> 篇
//分类
<?php echo $count_categories = wp_count_terms('category'); ?> 个
//标签
<?php echo $count_tags = wp_count_terms('post_tag'); ?> 个
//会员
<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?> 个
<?php global $wpdb; $users = $wpdb->get_var("select count(id) from $wpdb->users"); echo "$users"; ?>个
<?php global $wpdb; $users = $wpdb->get_var("select count(id) from $wpdb->users"); echo "目前我们已经有 ".'<span class="counter">' .$users.'</span>'." 位会员加入"; ?>
//链接
<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?> 个
//评论
<?php $total_comments = get_comment_count(); echo $total_comments['approved'];?> 条
//浏览
<?php get_totalviews(true, true, true); ?> 次
//运行
<?php echo floor((time()-strtotime("2010-04-02"))/86400);?>天
//更新
<?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y年n月j日', strtotime($last[0]->MAX_m));echo $last; ?>
您可以根据自己的需求自主调用。以上代码结束下方是另一种统计代码!
代码二
这下面的代码加入主题的functions.php里面
// 每周更新
function get_week_post_count(){
$date_query = array(
array(
'after'=>'1 week ago'
)
);$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'date_query' => $date_query,
'no_found_rows' => true,
'suppress_filters' => true,
'fields'=>'ids',
'posts_per_page'=>-1
);
$query = new WP_Query( $args );
return $query->post_count;
}
// 每周日新
function jiubgx() {
$today = getdate();
$query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"]);
$postsNumber = $query->found_posts;
echo $postsNumber;
}
调用代码:
下面的代码放到你要调用显示的地方。
//显示总文章数量
<?php echo $publish_posts = wp_count_posts()->publish;?>
//查询特定分类文章数量,比如我要查52分类
<?php echo publish;echo $publish_posts-get_category(52)->count;?>
//本周更新
<?php echo get_week_post_count(); ?>
//今日更新
<?php echo jiubgx();?>
利熙在用代码:
这下面的代码加入主题的functions.php里面
// 获取本周更新数量
function get_week_post_count(){
$date_query = array(
array(
'after'=>'1 week ago'
)
);$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'date_query' => $date_query,
'no_found_rows' => true,
'suppress_filters' => true,
'fields'=>'ids',
'posts_per_page'=>-1
);$query = new WP_Query( $args );return $query->post_count;
}
// 获得今日更新数量
function get_today_post_count(){
$date_query = array(
array(
'after'=>'1 day ago'
)
);$args = array(
'post_type' => 'post',
'post_status'=>'publish',
'date_query' => $date_query,
'no_found_rows' => true,
'suppress_filters' => true,
'fields'=>'ids',
'posts_per_page'=>-1
);$query = new WP_Query( $args );return $query->post_count;}
利熙在用调用代码:
本周更新:<?php echo get_week_post_count();?>篇
今日更新:<?php echo get_today_post_count();?>篇
声明:本站文章原创有部分资源来源于网络,如无特殊说明或标注。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系 admin@liitk.com 进行删除处理!。
评论(0)