“最受欢迎”列表帖子(基于视图数)

发布于 2024-08-12 09:57:13 字数 437 浏览 2 评论 0原文

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

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

[#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 模板标签来实现,我更喜欢使用第三方插件。但对我来说重要的是显示格式,我需要左侧的缩略图。

I'd like to show a list of most popular posts sorted by number of views.

I'd like to show small thumbnails (the same image(s) used in the posts just resized to fit) on left, and excerpt on right. So the formatting would look like:

[#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

Is there a plugin available for this? If can be achieved by simply using wordpress template tags, I'd prefer that over a 3rd party plugin. But important to me is display formatting, I need thumbnails on left.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷途知返 2024-08-19 09:57:13

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

$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
    }
}

You can install a plugin like Wordpress popular posts.
This plugin can't do exactly what you want, but it records pageviews in the database, and you can use that data in your theme by writing code like the following:

$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
    }
}
稀香 2024-08-19 09:57:13

如果您想尽可能避免使用第三方插件,这可能会有所帮助。它确实需要插件 - wp-postiews - 但仅存储页面浏览量。完成后,您可以使用下面的代码来完全自定义您显示的内容以及排序方式。我使用它的目的与您大致相同(我想在缩略图滑块中显示最受欢迎的帖子的缩略图)。

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

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

其中函数 popularPostsThumb() 将在下面定义(您可以更改要显示的帖子数量),但将返回以下内容列表元素,位于列表标记

  • 内。

    现在在您的 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;}
    

    在此示例中,它仅输出图像的缩略图 - 但在

  • 标记内您可以更改 HTML 以生成您想要的内容(加上一些 CSS 样式)。您可以轻松提取帖子的摘录 - 我还没有测试过它,但是像 $excerpt= $post->post_excerpt 这样的东西应该可以工作。(有一些“更简洁”的方法来获取缩略图帖子的标题 - 但这样您可以手动编辑 的标题)。

    希望这有帮助。

    If you wanted to avoid a third party plugin as much as possible than this might help. It does require a plug-in - wp-postiews - but only to store the number of page views. Once you've done that you can use the code below to fully customise what you display and how you sort it. I have used it for much the same purpose as you are (I wanted to display the thumbnails of the most popular posts in a thumbnail slider).

    Now, lets say you wanted to display your posts as a list:

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

    Where the function popularPostsThumb() will be defined below (and you can alter how many posts you want displayed), but will return the content of the list elements, inside the list tags <li></li>.

    Now define the above function in your functions.php file

    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;}
    

    In this example it just outputs the thumbnail of the image - but inside the <li></li> tags you can change the HTML to produce what you wanted (plus a some CSS styling). You can easily extract the excerpt of the post - I've not tested it, but something like $excerpt= $post->post_excerpt should work.(There are 'neater' ways of getting the thumbnail of the post - but this way you can manually edit the title of the <img>).

    Hope this helps.

    一抹微笑 2024-08-19 09:57:13

    找到了一个完美运行的插件

    Found a plugin that works perfectly.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文