熱門和熱評文章修改

3,782次閱讀
尚無留言

共計 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
評論(尚無留言)