子比主题美化侧边栏添加网站统计小工具

百度已收录

文章最后更新时间:2024-12-28 11:46:46

效果演示一

子比主题美化侧边栏添加网站统计小工具-氯化钠资源网站

部署代码

  1. 添加的地方有两处,function.php(主站链接/wp-content/themes/zibll/function.php)
  2. 后台 >> 外观 >> 小工具 >> 实时预览。
//实现侧边栏文本工具运行PHP代码
add_filter('widget_text', 'php_text', 99);
function php_text($text) {
if (strpos($text, '<' . '?') !== false) {
ob_start();
eval('?' . '>' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}
function zfunc_comments_users($postid=0,$which=0) {
  $comments = get_comments('status=approve&type=comment&post_id='.$postid); //获取文章的所有评论
  if ($comments) {
    $i=0; $j=0; $commentusers=array();
    foreach ($comments as $comment) {
      ++$i;
      if ($i==1) { $commentusers[] = $comment->comment_author_email; ++$j; }
      if ( !in_array($comment->comment_author_email, $commentusers) ) {
        $commentusers[] = $comment->comment_author_email;
        ++$j;
      }
    }
    $output = array($j,$i);
    $which = ($which == 0) ? 0 : 1;
    return $output[$which]; //返回评论人数
  }
  return 0; //没有评论返回0
};

function nd_get_all_view(){//文章总访问量
  global $wpdb;
  $count=0;
  $views= $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key='views'");
  foreach($views as $key=>$value){
    $meta_value=$value->meta_value;
    if($meta_value!=' '){
      $count+=(int)$meta_value;
    }
  }return $count;
}
<ul class="list-group box-shadow-wrap-normal">
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-map-o"></i> <span class="badge pull-right cursor" title="文章数目"><?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>篇</span> 文章数目</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-user-o"></i> <span class="badge pull-right cursor" title="注册用户"><?php global $wpdb;$users = $wpdb->get_var("select count(id) from $wpdb->users");echo "$users" ?>位</span> 注册用户</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-calendar-check-o"></i> <span class="badge pull-right cursor" title="运行天数"><?php echo floor((time()-strtotime("2024-10-9"))/86400); ?>天</span> 运行天数</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-comments-o"></i> <span class="badge pull-right cursor" title="评论数目"><?php echo zfunc_comments_users($post->ID); ?>句</span> 评论数目</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-street-view" title="总访问量"></i> <span class="badge pull-right cursor"><?php echo ''.nd_get_all_view().''; ?>次</span> 总访问量</li>
</ul>

配置演示

子比主题美化侧边栏添加网站统计小工具-氯化钠资源网站

PHP 代码只能在文本上输入,其他输入会出现错误现象

子比主题美化侧边栏添加网站统计小工具-氯化钠资源网站

点击文本,代码放到文本里即可

效果演示二

子比主题美化侧边栏添加网站统计小工具-氯化钠资源网站

自定义HTML(小工具的文本里)

<div class="gn_box"><center><span style="color: #e80017;">2</span><span style="color: #d1002e;">0</span><span style="color: #ba0045;">2</span><span style="color: #a3005c;">5</span><span style="color: #8c0073;">年</span><span style="color: #75008a;">-</span><span style="color: #5e00a1;">新</span><span style="color: #4700b8;">年</span><span style="color: #3000cf;">倒</span><span style="color: #1900e6;">计</span><span style="color: #0200fd;">时</span></center> 
<div id="CountMsg" class="HotDate"><span id="t_d">0 天</span>
<span id="t_h">0 时</span>
<span id="t_m">0 分</span>
<span id="t_s">0 秒</span></div>
<!-- 统计信息部分 -->
<ul class="list-group box-shadow-wrap-normal">
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-map-o"></i> <span class="badge pull-right cursor" title="文章数目"><?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>篇</span> 文章数目</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-user-o"></i> <span class="badge pull-right cursor" title="注册用户"><?php global $wpdb;$users = $wpdb->get_var("select count(id) from $wpdb->users");echo "$users" ?>位</span> 注册用户</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-calendar-check-o"></i> <span class="badge pull-right cursor" title="运行天数"><?php echo floor((time()-strtotime("2024-10-9"))/86400); ?>天</span> 运行天数</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-fw fa-comments-o"></i> <span class="badge pull-right cursor" title="评论数目"><?php echo zfunc_comments_users($post->ID); ?>句</span> 评论数目</li>
<li class="list-group-item text-second"><i class="xy-tab-icon fa fa-street-view" title="总访问量"></i> <span class="badge pull-right cursor"><?php echo ''.nd_get_all_view().''; ?>次</span> 总访问量</li>
</ul>
</div>
<script type="text/javascript"> function getRTime() {
        var EndTime = new Date('2025/01/1 00:00:00');
        var NowTime = new Date();
        var t = EndTime.getTime() - NowTime.getTime();
        var d = Math.floor(t / 1000 / 60 / 60 / 24);
        var h = Math.floor(t / 1000 / 60 / 60 % 24);
        var m = Math.floor(t / 1000 / 60 % 60);
        var s = Math.floor(t / 1000 % 60);
        document.getElementById("t_d").innerHTML = d + " 天";
        document.getElementById("t_h").innerHTML = h + " 时";
        document.getElementById("t_m").innerHTML = m + " 分";
        document.getElementById("t_s").innerHTML = s + " 秒";
    }
    setInterval(getRTime, 1000);
    </script>

自定义样式

子比主题美化侧边栏添加网站统计小工具-氯化钠资源网站
    .HotDate {
    display: flex;
    justify-content: center;
    font-weight: bold;
    color: #333;
}

    .list-group-item {
        font-size: 16px;
        
        border: 1px solid #ddd;
        border-radius: 6px;
       
        background-color: #fff;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
    }

    .list-group-item i {
        margin-right: 10px;
        color: #007bff;
    }

    .list-group-item .badge {
        background-color: #28a745;
        font-size: 14px;
    }

    .list-group-item.text-second {
        background-color: #f8f9fa;
    }

    .list-group-item:hover {
        background-color: #f1f1f1;
    }

本文链接:https://lzphy.top/1339/
© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容