熱門和熱評文章修改

3,283次閱讀
尚無留言

共计 3853 个字符,预计需要花费 10 分钟才能阅读完成。

熱門文章

  • 打開 Fuctions.php 新增下述程式碼
/* 文章瀏覽次數統計 */
function record_visitors()
{if (is_singular())
  {
    global $post;
    $post_ID = $post->ID;
    if($post_ID)
    {$post_views = (int)get_post_meta($post_ID, 'views', true);
      if(!update_post_meta($post_ID, 'views', ($post_views+1)))
      {add_post_meta($post_ID, 'views', 1, true);
      }
    }
  }
}
add_action('wp_head', 'record_visitors');
 
function post_views($before = '( 點擊', $after = '次)', $echo = 1)
{
  global $post;
  $post_ID = $post->ID;
  $views = (int)get_post_meta($post_ID, 'views', true);
  if ($echo) echo $before, number_format($views), $after;
  else return $views;
}
/* 文章瀏覽次數統計結束 */
  • 打開 index.php(選擇要顯示的頁面) 新增下述程式碼
<span class="meat_span"><i class="iconfont">&#279;</i> 瀏覽 <?php post_views('',' 次 '); ?></span>

上述主要是進行文章點擊數的統計,透過 <?php post_views(‘ ‘, ‘ 次 ’); ?>  進行呼叫的概念,大部分會將 post_views 這個函式放置在 index、archive、category、signal 這些 php 頁面當中進行使用。後面繼續講述如何寫成小工具統計排名點擊率前 10 名的文章。

 

  • 打開 Fuctions.php 新增下述程式碼
/// get_most_viewed_format
/// 取得閱讀最多的文章
function get_most_viewed_format($mode = '', $limit = 10,$term_id = 0) {
  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> <a href='.$get_permalink.'class="title"title='.$post_title.'>'.$post_title.'</a></li>';
    }
  } else {$output = "<li>N/A</li>\n";}
  echo $output;
}
  • 打開 Sidebar.php(想要呈現的側邊欄) 新增下述程式碼
<?php get_most_viewed_format(); ?>
  • 我的部落格主要使用動態側邊欄來進行顯示所以完整的程式碼如下
<div class="widget_tabcontent">
    <h3>
		<span class="selected"> 最新文章 </span> 
		<span> 熱門文章 </span>
		<span> 隨機文章 </span>
	</h3>
    <ul>
        <?php $post_query = new WP_Query('showposts=10'); while ($post_query->have_posts()) : $post_query->the_post(); $do_not_duplicate = $post->ID; ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_title(); ?>
            </a>
        </li>
        <?php endwhile;?>
    </ul>
    <ul class="hide">
        <?php get_most_viewed_format(); ?>
    </ul>
    <ul class="hide">
        <?php $rand_posts = get_posts('numberposts=10&orderby=rand');  foreach($rand_posts as $post) : ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_title(); ?>
            </a>
        </li>
        <?php endforeach; ?>
    </ul>
</div>

參考資料:

WordPress 非插件添加文章浏览次数统计功能

WordPress 非插件版 post_view 实现文章点击量统计功能

怎样给 wordpress CMS 主题添加热点文章?

  • 1 2
正文完
 0
評論(尚無留言)

夜升筆談

夜升筆談
夜升筆談
視寫代碼為信仰,奉高效能為執著,成為大牛不是一蹴可幾,但只要秉持信念終究能成,我依舊在這條路上不斷前進。
最新评论
Yosheng Yosheng 我是买梯子了 只是理解一下原理而已
虚拟信用卡 虚拟信用卡 还是直接买梯子吧,太浪费时间了
spring spring 学习了
cskepper cskepper 非常有用,已经顺利解决
cskepper cskepper 非常有用,已经顺利解决
Yosheng Yosheng 這可能是代理服務器的問題了...
淑淑 淑淑 我在大陸使用了VPN 照著你說的操作 但是出現無法透過代理伺服器連線 我的電腦是W7 也下載了兼容版 用了大概一個星期後今天LINE就無法使用VPN了 這種要怎樣解決
Yosheng Yosheng 如果不能直接撥放建議直接下載回來播放,後面我就沒去折騰為什麼不能線上播放了
SADFISH SADFISH 我无法播放语音是为什么呀楼主
Yosheng Yosheng 微信号 yosheng0323