wordpress(知更鸟模板)实现文章阅读量的显示|时刻需

阅读次数: 5,555

  • A+
所属分类:建站

1.登录后台 外观=>编辑,然后找到function.php

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');

将上边的代码写入function.php中

2.修改详情页 template/content.php

<header class="entry-header">
		<?php if ( is_single() ) : ?>
			<?php if ( get_post_meta($post->ID, 'header_img', true) ) { ?>
				<div class="entry-title-clear"></div>
			<?php } else { ?>
				<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
			<?php } ?>
		<?php else : ?>
			<?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>
		<?php endif; ?>
		<h2 align="right">阅读次数: <?php get_post_views($post -> ID); ?></h2>
	</header><!-- .entry-header -->

修改为如上所写。


在知更鸟的主题上 右上角的《编辑菜单》四个字去掉

不多说直接上图

wordpress(知更鸟模板)实现文章阅读量的显示|时刻需

这样就可以了

时刻需

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: