首先在网站后台的wordpress模板函数functions.php文件中加入以下的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*显示文章浏览次数*/
function getPostViews($postID){
$count = get_post_meta($postID,'views', true);
if($count==''){
delete_post_meta($postID,'views');
add_post_meta($postID,'views', '0');
return "0";
}
return $count.'';
}
function setPostViews($postID) {
$count = get_post_meta($postID,'views', true);
if($count==''){
$count = 0;
delete_post_meta($postID,'views');
add_post_meta($postID,'views', '0');
}else{
$count++;
update_post_meta($postID,'views', $count);
}
}

在需要显示浏览量的地方,包括首页,分类页,文章页都可以使用。

1
2
<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?>

WordPress网站制作热门文章排行榜(浏览量排序)

在需要调用按浏览量排序的热门文章位置,使用以下的代码进行调用文章列表;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

<ul>
<?php
$args = array(
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'posts_per_page' => 6,
'order' => 'DESC'
);
query_posts($args);
while (have_posts()) : the_post();

// 获取文章所属栏目
$categories = get_the_category();
$category_name = '';
$category_link = '';

if (!empty($categories)) {
$first_category = $categories[0]; // 获取第一个栏目
$category_name = $first_category->name;
$category_link = get_category_link($first_category->term_id);
}
?>
<li>
<!-- 显示栏目信息 -->
<?php if ($category_name) : ?>
<span class="post-category">
<a href="<?php echo $category_link; ?>"><?php echo $category_name; ?></a>
</span>
<?php endif; ?>

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="kc-view fright">
浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?>
</span>
</li>
<?php
endwhile;
wp_reset_query();
?>
</ul>

这样就可以调用出用户浏览最多的6篇文章了。

按浏览量排序:使用 meta_key => ‘views’ 和 orderby => ‘meta_value_num’

备用方案:如果按浏览量查询无结果,自动切换到按日期排序

分类限制:只显示分类ID为6的文章

数量限制:显示6篇文章

调用分类为2,热门的3篇文章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<ul>
<?php
$args = array(
'cat' => 2, // 指定栏目ID为2
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'posts_per_page' => 3, // 显示3篇文章
'order' => 'DESC'
);
query_posts($args);
while (have_posts()) : the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="kc-view fright">
浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?>
</span>
</li>
<?php
endwhile;
wp_reset_query();
?>
</ul>

调用热门文章,双循环列表案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<!-- 热门文章 -->
<section class="section editors-picks mb-20">
<div class="title-wrap">
<h3 class="section-title">热门文章</h3>
<a href="#" class="all-posts-url">查看全部</a>
</div>
<div class="row">
<?php
// 获取全站热门文章(按浏览量排序)
$args = array(
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'posts_per_page' => 7, // 获取7篇文章(1篇大图 + 6篇小图)
'order' => 'DESC'
);

$hot_posts = new WP_Query($args);

if ($hot_posts->have_posts()) {
$count = 0;

while ($hot_posts->have_posts()) {
$hot_posts->the_post();
$count++;

// 第一篇文章显示为大图布局
if ($count == 1) {
?>
<div class="col-lg-7">
<article class="entry">
<div class="entry__img-holder">
<a href="<?php the_permalink(); ?>">
<div class="thumb-container thumb-75">
<?php if (has_post_thumbnail()) : ?>
<img data-src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'large'); ?>"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php else : ?>
<img data-src="<?php echo get_template_directory_uri(); ?>/static/picture/default.jpg"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php endif; ?>
</div>
</a>
</div>
<div class="entry__body">
<div class="entry__header">
<?php
$categories = get_the_category();
if (!empty($categories)) {
$first_category = $categories[0];
echo '<a href="' . get_category_link($first_category->term_id) . '" class="entry__meta-category">' . esc_html($first_category->name) . '</a>';
}
?>
<h2 class="entry__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<ul class="entry__meta">
<li class="entry__meta-author"><i class="ui-author"></i><a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a></li>
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
<li class="entry__meta-comments"><i class="ui-comments"></i><a href="<?php comments_link(); ?>"><?php comments_number('0', '1', '%'); ?></a></li>
<li><span class="view-count"><?php echo number_format(getPostViews(get_the_ID())); ?></span></li>
</ul>
</div>
<div class="entry__excerpt">
<p><?php echo wp_trim_words(get_the_excerpt(), 30, '...'); ?></p>
</div>
</div>
</article>
</div>
<div class="col-lg-5">
<ul class="post-list-small">
<?php
} else {
// 后续文章显示为小图列表
?>
<li class="post-list-small__item">
<article class="post-list-small__entry">
<div class="post-list-small__img-holder">
<?php if (has_post_thumbnail()) : ?>
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/static/picture/default-thumb.jpg" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php endif; ?>
</div>
<div class="post-list-small__body">
<h3 class="post-list-small__entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<ul class="entry__meta">
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
<li><span class="view-count"><?php echo number_format(getPostViews(get_the_ID())); ?></span></li>
</ul>
</div>
</article>
</li>
<?php
}
}
?>
</ul>
</div>
<?php
} else {
echo '<div class="col-12"><p>暂无热门文章</p></div>';
}

wp_reset_postdata();
?>
</div>
</section>
<!-- end 热门文章 -->

实现栏目为2的热门文章,调用7篇文章,双循环代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!-- 栏目2热门文章 -->
<section class="section editors-picks mb-20">
<div class="title-wrap">
<h3 class="section-title">栏目2热门文章</h3>
<a href="<?php echo get_category_link(2); ?>" class="all-posts-url">查看全部</a>
</div>
<div class="row">
<?php
// 获取栏目2的热门文章(按浏览量排序)
$args = array(
'cat' => 2, // 栏目ID为2
'meta_key' => 'views',
'orderby' => 'meta_value_num',
'posts_per_page' => 7, // 获取7篇文章(1篇大图 + 6篇小图)
'order' => 'DESC'
);

$hot_posts = new WP_Query($args);

if ($hot_posts->have_posts()) {
$count = 0;

while ($hot_posts->have_posts()) {
$hot_posts->the_post();
$count++;

// 第一篇文章显示为大图布局
if ($count == 1) {
?>
<div class="col-lg-7">
<article class="entry">
<div class="entry__img-holder">
<a href="<?php the_permalink(); ?>">
<div class="thumb-container thumb-75">
<?php if (has_post_thumbnail()) : ?>
<img data-src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'large'); ?>"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php else : ?>
<img data-src="<?php echo get_template_directory_uri(); ?>/static/picture/default.jpg"
src="<?php echo get_template_directory_uri(); ?>/static/picture/empty.png"
class="entry__img lazyload" alt="<?php the_title_attribute(); ?>" />
<?php endif; ?>
</div>
</a>
</div>
<div class="entry__body">
<div class="entry__header">
<?php
$categories = get_the_category();
if (!empty($categories)) {
$first_category = $categories[0];
echo '<a href="' . get_category_link($first_category->term_id) . '" class="entry__meta-category">' . esc_html($first_category->name) . '</a>';
}
?>
<h2 class="entry__title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<ul class="entry__meta">
<li class="entry__meta-author"><i class="ui-author"></i><a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a></li>
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
<li class="entry__meta-comments"><i class="ui-comments"></i><a href="<?php comments_link(); ?>"><?php comments_number('0', '1', '%'); ?></a></li>
<li><span class="view-count"><?php echo number_format(getPostViews(get_the_ID())); ?></span></li>
</ul>
</div>
<div class="entry__excerpt">
<p><?php echo wp_trim_words(get_the_excerpt(), 30, '...'); ?></p>
</div>
</div>
</article>
</div>
<div class="col-lg-5">
<ul class="post-list-small">
<?php
} else {
// 后续文章显示为小图列表
?>
<li class="post-list-small__item">
<article class="post-list-small__entry">
<div class="post-list-small__img-holder">
<?php if (has_post_thumbnail()) : ?>
<img src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?>" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/static/picture/default-thumb.jpg" class="post-list-small__img" alt="<?php the_title_attribute(); ?>">
<?php endif; ?>
</div>
<div class="post-list-small__body">
<h3 class="post-list-small__entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<ul class="entry__meta">
<li class="entry__meta-date"><i class="ui-date"></i> <?php the_time('Ynj日'); ?></li>
<li><span class="view-count"><?php echo number_format(getPostViews(get_the_ID())); ?></span></li>
</ul>
</div>
</article>
</li>
<?php
}
}
?>
</ul>
</div>
<?php
} else {
echo '<div class="col-12"><p>栏目2暂无热门文章</p></div>';
}

wp_reset_postdata();
?>
</div>
</section>
<!-- end 栏目2热门文章 -->