WordPress 接入百度主动推送、增加访问统计
一般我们的网站新动态的时候,百度并不会收录,百度主动来爬取我们的内容往往又需要较长的时间,所以百度提供了主动推送,大家可以去那里提交我们的数据,当然我们还有另外的方法,将推送代码嵌入我们的网站,更方便快捷。
由于我这边是Wordpress博客网站,需要百度收录,而博客环境基于php,所以这里使用php的方式嵌入。
1.找到我们当前的主题
wordpress/wp-content/themes/当前主题/functions.php
2.在functions.php文件加入下面内容:
function Baidu_Submit($post_ID){ $urls = get_permalink($post_ID); $api = 'http://data.zz.baidu.com/urls?site=https://需要提交的域名&token=你的KEY'; $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), ); curl_setopt_array($ch, $options); $result = curl_exec($ch); echo $result; } add_action('publish_post', 'Baidu_Submit',0);
增加访问统计,由于网上资料太少而又不全,后来通过F12分析网站元素找到了修改方法,修改方法如下:
1.加入如下代码到functions.php
//postviews function get_post_views ($post_id) { $count_key = 'views'; $count = get_post_meta($post_id, $count_key, true); if ($count == '') { delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); $count = '0'; } echo number_format_i18n($count); } function set_post_views () { global $post; $post_id = $post -> ID; $count_key = 'views'; $count = get_post_meta($post_id, $count_key, true); if (is_single() || is_page()) { if ($count == '') { delete_post_meta($post_id, $count_key); add_post_meta($post_id, $count_key, '0'); } else { update_post_meta($post_id, $count_key, $count + 1); } } } add_action('get_header', 'set_post_views');
编辑下面php文件:
wordpress/wp-content/themes/当前主题//framework/templates/blog/listing-post.php
$comments_number = get_comments_number();段落后加入如下内容:
$meta_html['number'] .= '<span class="w-blog-post-meta-comments">'; // TODO Replace with get_comments_popup_link() when https://core.trac.wordpress.org/ticket/17763 is resolved ob_start(); echo get_post_views(get_the_ID()).'次阅读'; $meta_html['number'] .= ob_get_clean(); $meta_html['number'] .= '</span>';
感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
暂无评论内容