新聞中心
如何獲得WordPress獲取瀏覽次數(shù)最多的文章
作者 / 無(wú)憂主機(jī) 時(shí)間 2018-01-08 18:52:15
不久之前無(wú)憂主機(jī)小編分享過(guò):如何獲得WORDPRESS文章瀏覽次數(shù)的統(tǒng)計(jì),文章所提供的方法是通過(guò)代碼方式,而不是通過(guò)插件來(lái)實(shí)現(xiàn)。不推薦使用插件相信這個(gè)原因說(shuō)過(guò)無(wú)數(shù)次就是減輕php空間負(fù)載,從而達(dá)到網(wǎng)站加速效果。今天無(wú)憂主機(jī)小編同樣也是通過(guò)免插件的方式介紹一下如何獲得WordPress獲取瀏覽次數(shù)最多的文章方法,以下的方法比插件還好,不但會(huì)獲得的文章瀏覽次數(shù)統(tǒng)計(jì)還會(huì)連搜索引擎爬蟲(chóng)也會(huì)進(jìn)行統(tǒng)計(jì)。 如何獲得WordPress獲取瀏覽次數(shù)最多的文章計(jì)的方法如下: 首先是點(diǎn)擊進(jìn)入到我們的控制面板,然后找到文件管理選項(xiàng),如圖所示: 點(diǎn)擊進(jìn)入之后找到我們站點(diǎn)的根目錄,如圖所示: 進(jìn)入到這個(gè)目錄之后找到我們的程序的路徑:當(dāng)前主題目錄下php文件,點(diǎn)擊編輯,如圖所示: 添加代碼操作:
/// 函數(shù)作用:取得閱讀最多的文章 function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') { global $wpdb, $post; $output = ''; $mode = ($mode == '') ? 'post' : $mode; $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : ''; $term_sql = (is_array($term_id)) ? "AND $wpdb->term_taxonomy.term_id IN (" . join(',', $term_id) . ')' : ($term_id != 0 ? "AND $wpdb->term_taxonomy.term_id = $term_id" : ''); $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : ''; $inr_join = $term_id ? "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)" : ''; // database query $most_viewed = $wpdb->get_results("SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = 'publish' AND post_password = '' $term_sql $type_sql AND meta_key = 'views' GROUP BY ID ORDER BY views DESC LIMIT $limit"); if ($most_viewed) { foreach ($most_viewed as $viewed) { $post_ID ???= $viewed->ID; $post_views = number_format($viewed->views); $post_title = esc_attr($viewed->post_title); $get_permalink = esc_attr(get_permalink($post_ID)); $output .= "<li>$beforetitle$post_title$aftertitle"; if ($show_date) { $posted = date(get_option('date_format'), strtotime($viewed->post_date)); $output .= "$beforedate $posted $afterdate"; } $output .= "$beforecount $post_views $aftercount</li>"; } } else { $output = "<li>N/A</li>n"; } echo $output; } ?添加完代碼效果圖,如圖所示: 最后我們?cè)谛枰@取瀏覽次數(shù)最多的文章的地方使用下面的代碼調(diào)用即可:
<?php get_most_viewed_format(); ?>
本文地址:http://www.gle-technology.com/wordpress/26104.html