WordPress站点统计功能代码

2017/11/21 12:07:27admin0 阅读11 评论

Wordpress 本身是没有运行天数、访客数统计等站点统计功能的,而站点统计却是能够直观展示站点运行状态的好功能,所以就可以自行加入该功能。日常小改主题期间,就自己也忍不住研究了下,即使轩陌大大已经弄了些,但自己还是想在加点什么<img src=“https://moshanghua.oss-cn-shanghai.aliyuncs.com/emoji/paopao/nidongde.png” alt=“@你懂的” />,那么下面我就将在百度上找到的一些经常使用到的函数罗列出来,方便自己改时参考,也奉献给大家看看(也许还有其他的函数,欢迎留言评论留下<img src=“https://moshanghua.oss-cn-shanghai.aliyuncs.com/emoji/alu/koushui.png” alt=“@口水” />)!

<h2>1、日志总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>

</code></pre>

<h2>2、草稿数目:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts-&gt;draft; ?&gt;  

</code></pre>

<h2>3、评论总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php echo $wpdb-&gt;get_var(&quot;SELECT COUNT(*) FROM $wpdb-&gt;comments&quot;);?&gt;  

</code></pre>

<h2>4、建站天数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php echo floor((time()-strtotime(&quot;2016-08-18&quot;))/86400); ?&gt;  

</code></pre>

<div class=“cue-blue” >
<p>记得将上述日期修改为你的建站日期</p>
</div>

<h2>5、标签总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php echo $count_tags = wp_count_terms(&#x27;post_tag&#x27;); ?&gt;  

</code></pre>

<h2>6、页面总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php $count_pages = wp_count_posts(&#x27;page&#x27;); echo $page_posts = $count_pages-&gt;publish; ?&gt;  

</code></pre>

<h2>7、分类总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php echo $count_categories = wp_count_terms(&#x27;category&#x27;); ?&gt;  

</code></pre>

<h2>8、链接总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php $link = $wpdb-&gt;get_var(&quot;SELECT COUNT(*) FROM $wpdb-&gt;links WHERE link_visible = &#x27;Y&#x27;&quot;); echo $link; ?&gt;  

</code></pre>

<h2>9、用户总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php $users = $wpdb-&gt;get_var(&quot;SELECT COUNT(ID) FROM $wpdb-&gt;users&quot;); echo $users; ?&gt;  

</code></pre>

<h2>10、最后更新:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>

&lt;?php $last = $wpdb-&gt;get_results(&quot;SELECT MAX(post_modified) AS MAX_m FROM $wpdb-&gt;posts WHERE (post_type = &#x27;post&#x27; OR post_type = &#x27;page&#x27;) AND (post_status = &#x27;publish&#x27; OR post_status = &#x27;private&#x27;)&quot;);$last = date(&#x27;Y-n-j&#x27;, strtotime($last[0]-&gt;MAX_m));echo $last; ?&gt;  

</code></pre>

<h2>11、访问总数:</h2>

<pre class=“line-numbers language-php”><code class=“language-php”>
<?php
$counterFile = "counter.txt";
$counterBackupFile = "counter_bak.txt";
function displayCounter($counterFile, $counterBackupFile) {
fp=fopen(fp = fopen(counterFile, "r");
num=fgets(num = fgets(fp, 10);
fclose($fp);
fp=fopen(fp = fopen(counterBackupFile, "r");
numBak=fgets(numBak = fgets(fp, 10);
fclose($fp);
if ($num < 10) {
if ($numBak > 10) {
$num = $numBak;
}
}
if (!is_user_logged_in()) {
$num += 1;
fp=fopen(fp = fopen(counterFile, "w");
fputs($fp, $num, 10);
fclose($fp);
if ($num % 20 == 0 && $num > 10) {
fp=fopen(fp = fopen(counterBackupFile, "w");
fputs($fp, $num, 10);
fclose($fp);
}
}
echo "$num"." 人次";
}

        if (!file_exists($counterFile)) {  
            fopen($counterFile, &quot;w&quot;);  
            fputs($fp, 0, 10);  
            fclose($fp);  
        }  
        if (!file_exists($counterBackupFile)) {  
            fopen($counterBackupFile, &quot;w&quot;);  
            fputs($fp, 0, 10);  
            fclose($fp);  
        }  

        displayCounter($counterFile, $counterBackupFile);  
    ?&gt;

</code></pre>

<h2>最后</h2>

简单的介绍下食用方法

参考上面的函数,找到正在使用的主题的文件夹内的 sidebar.php 打开,创建一个 div ,用li来包裹函数,如果加运行天数的话,那里面的日期改为你的建站日期即可。然后保存。如果你嫌样式单调了,就加点css美化下,大功告成!(代码和方法都给了,再不会用的话…)

<pre class=“line-numbers language-html”><code class=“language-html”>
<div>
<h1>站点统计</h1>
<ul>
<li>文章总数:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?> 篇</li>
<li>页面总数:<?php echo wp_count_posts('page')->publish;?> 个</li>
<li>评论总数:<?php echo wp_count_comments()->total_comments?> 条</li>
<li>分类总数:<?php echo wp_count_terms('category')?> 个</li>
<li>标签总数:<?php echo wp_count_terms('post_tag')?> 个</li>
<li>运行天数:<?php echo floor((time()-strtotime("2014-04-05"))/86400);?> 天</li>
</ul>
</div>
<!–函数也可以这样写–>
<div>
<h1>站点统计</h1>
<ul>
<li>文章总数:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish . '篇'; ?></li>
<li>页面总数:<?php echo wp_count_posts('page')->publish . '个';?></li>
<li>评论总数:<?php echo wp_count_comments()->total_comments . '条';?></li>
<li>分类总数:<?php echo wp_count_terms('category') . '个';?></li>
<li>标签总数:<?php echo wp_count_terms('post_tag') . '个';?></li>
<li>运行天数:<?php echo floor((time()-strtotime("2014-04-05"))/86400) . '天';?></li>
</ul>
</div>
</code></pre>

评论区

  • 轩陌#4
    轩陌2018/5/10 12:41:29

    [guzhang]

    macOSChrome

  • YawZhou#3
    YawZhou2017/11/22 09:03:02

    <code>
    from future import you

    autumn = {
    ‘breezing’:True,
    ‘length’:10
    }

    i.sort([autumn, you]) == [you, autumn]

    //秋风十里不如你
    </code>

    WindowsChrome

    • xiaomo#5
      xiaomo2017/11/22 13:06:26
      @YawZhou

      [bangbangtang] [xieyanxiao]

      WindowsFirefox

    • YawZhou#4
      YawZhou2017/11/22 13:04:59
      @xiaomo

      [shuai]

      WindowsChrome

    • xiaomo#3
      xiaomo2017/11/22 13:04:21
      @YawZhou

      咳咳,声明下啊,我不搞基啊 [xiaoku]

      WindowsFirefox

    • YawZhou#2
      YawZhou2017/11/22 13:01:52
      @xiaomo

      666 [haobang]

      WindowsChrome

    • xiaomo#1
      xiaomo2017/11/22 13:00:50
      @YawZhou

      if(mountain.arris==None):

      if(river.water==None):

      if(winter.thunder==True):

      if(summer.snow==True):

      if(sky.height==ground.height):

      i.withyou=False

      else:

      i.withyou=True

      #山无陵, 江水为竭, 冬雷震震, 夏雨雪, 天地合, 乃敢与君绝!

      WindowsFirefox

  • 淘气#2
    淘气2017/11/21 23:21:07

    东西是个好东西,可惜我已经弃坑了 [baiyan]

    AndroidChrome

    • xiaomo#1
      xiaomo2017/11/22 12:52:33
      @淘气

      干嘛要弃坑呢,wp不是很好嘛 [xieyanxiao]

      WindowsFirefox

  • c0smxsec#1
    c0smxsec2017/11/21 23:15:30

    不敢搞这些,怕看到那可怜的数字使我伤心

    AndroidChrome

    • xiaomo#1
      xiaomo2017/11/22 12:54:48
      @c0smxsec

      O(∩_∩)O哈哈~,一样啦,每天就那么几个可冷的数字在那躺着,要不是你们这些老邻居有时来看看我,估计都没数了Orz ヾ(・|

      WindowsFirefox