我正在尝试在其上获得热门帖子部分的网站上工作。我已经尝试过插件,但是它们需要wp_head()并且会破坏我在网站上的jCarousel。我已经在functions.php中使用以下代码将代码实现到getPostViews:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}
else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
然而,当我在循环中插入以下内容以显示它时,我的网站告诉我需要再次安装wordpress。当我完成安装时,我得到了一个数据库错误列表。但是,如果我在当天晚些时候回到它并删除了代码,那么该网站将再次运行。
<? query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC'); ?>
如何使用此功能获得热门帖子?或者我是否应该采用另一种方式让热门帖子发挥作用?谢谢你的帮助。
答案 0 :(得分:1)
在functions.php:
中说明这一点function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}
else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
接下来,将此位置放在您希望返回带有视图的帖子列表的位置:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<li><?php setPostViews(get_the_ID()); echo getPostViews(get_the_ID());; ?></li>
<?php endforeach; ?>
</ul>