【问题标题】:List of 'most popular' posts (based on # of views)“最受欢迎”帖子列表(基于查看次数)
【发布时间】:2010-12-19 05:17:50
【问题描述】:

我想显示按浏览次数排序的最受欢迎帖子列表。

我想在左侧显示小缩略图(帖子中使用的相同图像,刚刚调整大小以适应),在右侧显示摘录。所以格式看起来像:

[#1 Post Title]
[80x80 thumbnail] [excerpt, limit to x chars]

[#2 Post Title]
[80x80 thumbnail] [excerpt, limit to x chars]

[#3 Post Title]
[80x80 thumbnail] [excerpt, limit to x chars]

... up to 5 posts

有可用的插件吗?如果可以通过简单地使用 wordpress 模板标签来实现,我更喜欢使用 3rd 方插件。但对我来说重要的是显示格式,我需要左侧的缩略图。

【问题讨论】:

  • 为什么?不是“编程相关”吗?

标签: wordpress sorting thumbnails categories


【解决方案1】:

您可以安装像Wordpress popular posts 这样的插件。 这个插件不能完全满足你的要求,但它会在数据库中记录页面浏览量,你可以通过编写如下代码在你的主题中使用该数据:

$right_now = gmdate("Y-m-d");

$max_most_read = 5; // Number of "most read-spots" 

$qstr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, 
    (select postid, sum(pageviews) pageviews 
     from $pageviews_table 
     where day >= '$right_now' - INTERVAL 30 DAY
     group by postid) pv
    WHERE wposts.post_status = 'publish' 
    AND wposts.post_type = 'post'
    AND wposts.ID = pv.postid
    AND wposts.post_date >= '$right_now' - INTERVAL 30 DAY
    ORDER BY pv.pageviews DESC
    LIMIT 0, " . $max_most_read . "
 ";

$posts = $wpdb->get_results($qstr);
if ($posts) {
    foreach ($posts as $post) {
    setup_postdata($post);
        $category = get_the_category();

        # Now you can use $post->post_content to extract image tag and excerpt
        # See http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it
        # on how to extract and resize first image
    }
}

【讨论】:

    【解决方案2】:

    如果您想尽可能避免使用第三方插件,这可能会有所帮助。它确实需要一个插件 - wp-postiews - 但仅用于存储页面浏览量。完成后,您可以使用下面的代码来完全自定义显示的内容以及排序方式。我已将它用于与您相同的目的 (I wanted to display the thumbnails of the most popular posts in a thumbnail slider)。

    现在,假设您想将帖子显示为列表:

        <ul>
          <?php echo get_popular_thumb(5); ?>
        </ul>
    

    函数popularPostsThumb() 将在下面定义(您可以更改要显示的帖子数量),但将返回列表元素的内容,在列表标签​​&lt;li&gt;&lt;/li&gt; 内。

    现在在 functions.php 文件中定义上述函数

    function get_popular_thumb($limit=10) {
        global $wpdb;
        $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0)  AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_type='post' AND post_date < '".current_time('mysql')."' AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
    
        if($most_viewed) {
            foreach ($most_viewed as $post) {
                $id = $post->ID;
                $post_views = intval($post->views);
                $post_title = get_the_title($post);
                $post_title = $post->post_title;
                $related_thumbnail = get_post_meta($post->ID, "thumbnail_url", $single = true);
                $thumb_src = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post->ID ));
    
            if (has_post_thumbnail($id )){
                $output .= '<li><a href="' . get_permalink($id) . '" title="'. $post_title . '"><img src="'. $thumb_src[0].'" title="'. $post_title .'"/>  </a> </li>';
            }
        }
    }
    return $output;}
    

    在本例中,它只输出图像的缩略图 - 但在 &lt;li&gt;&lt;/li&gt; 标签内,您可以更改 HTML 以生成您想要的内容(加上一些 CSS 样式)。您可以轻松地提取帖子的摘录 - 我还没有测试过,但 $excerpt= $post-&gt;post_excerpt 之类的东西应该可以工作。(有“更简洁”的方式来获取帖子的缩略图 - 但这样你可以手动编辑标题&lt;img&gt;)。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      找到了一个完美运行的plugin

      【讨论】:

        猜你喜欢
        • 2014-04-13
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多