【发布时间】:2015-04-18 10:07:07
【问题描述】:
我试图在 wordpress 的 sidebar.php 中显示我浏览次数最多的帖子。
我尝试了以下代码,但它会重复显示一张特色图片 3 次。
这是link
我的代码是:
sidebar.php
<div class="popular">
<h2>Most Popular Posts</h2>
<?php echo popularPosts(3); ?>
</div>
function.php
<?php
function popularPosts($num) {
global $wpdb;
$posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");
foreach ($posts as $post) {
setup_postdata($post);
$id = $post->ID;
$title = $post->post_title;
$count = $post->comment_count;
if ($count != 0) {
$popular .= '<li>';
$popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title .the_post_thumbnail(). '</a> ';
$popular .= '</li>';
}
}
return $popular;
}
?>
我使用以下代码计算我的帖子浏览量。
function bac_PostViews($post_ID) {
//Set the name of the Posts Custom Field.
$count_key = 'post_views_count';
//Returns values of the custom field with the specified key from the specified post.
$count = get_post_meta($post_ID, $count_key, true);
//If the the Post Custom Field value is empty.
if($count == '')
{
$count = 0; // set the counter to zero.
//Delete all custom fields with the specified key from the specified post.
delete_post_meta($post_ID, $count_key);
//Add a custom (meta) field (Name/value)to the specified post.
add_post_meta($post_ID, $count_key, '0');
return $count . ' View';
//If the the Post Custom Field value is NOT empty.
}else{
$count++; //increment the counter by 1.
//Update the value of an existing meta key (custom field) for the specified post.
update_post_meta($post_ID, $count_key, $count);
//If statement, is just to have the singular form 'View' for the value '1'
if($count == '1')
{
return $count . ' View';
}
//In all other cases return (count) Views
else
{
return $count . ' Views';
}
}
}
?>
请建议我如何使用特色图片和帖子标题显示三个最受欢迎的页面。
【问题讨论】:
-
试试
get_the_post_thumbnail($id)而不是the_post_thumbnail() -
现在工作....非常感谢