PHP - 在 WordPress 中创建统计文章浏览量的简码
第 1 步:
为您的文章(POST)/ 页面(PAGE)创建 PODS 自定义字段
创建一个数字(NUMBER)字段
将其命名为 page_view(不要使用大写字母)
第 2 步:
在您的主题目录中找到 functions.php 文件 > wp-content \ themes \ your-theme \functions.php
第 3 步:
使用以下 PHP 代码
<pre data-lang="PHP"><code>function create_view_counter(){
/page_view will be the name of field created in PODS/
$view = get_post_meta(get_the_ID(), 'page_view', true);
/ check if your field is empty /
if ($view == ''){
$view = 0;
}
/auto add 1 view to your counter /
$view = $view+1;
/update your counter back to WP database /
update_post_meta(get_the_ID(), 'page_view', $view);
/display the counter in your page /
return "View: $view";
}
//create the shortcode by the function above
add_shortcode ('viewcounter', 'create_view_counter');</code></pre>
第 4 步:
将简码添加到您的文章内容中(如果不添加则不会进行统计),因此最好将其放置在您希望统计的位置。例如主题模板或页脚(footer)等……
[viewcounter]