最多评论/热门帖子和偏移量

时间:2012-10-22 作者:Andy M

我使用此代码根据评论数显示3个最受欢迎的帖子:

<?php
            $pop_posts = 3;
                $popularposts = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title,  COUNT($wpdb->comments.comment_post_ID) AS \'stammy\' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = \'1\' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = \'publish\' AND comment_status = \'open\' GROUP BY $wpdb->comments.comment_post_ID ORDER BY stammy DESC LIMIT ".$pop_posts;
                $posts = $wpdb->get_results($popularposts);
                if($posts){
                    foreach($posts as $post){
                        $post_title = stripslashes($post->post_title);
                        $guid = get_permalink($post->ID);
                        $thumb = get_post_meta($post->ID,\'_thumbnail_id\',false);
                        $thumb = wp_get_attachment_image_src($thumb[0], \'popular-posts-image\', false);
                        $thumb = $thumb[0]; 
        ?>

                            <?php if ($thumb) { ?>
                                <img src="<?php echo $thumb; ?>" width=80 height=80 />
                            <?php } ?>
                            <h4><a href="<?php echo $guid; ?>" title="<?php echo $post_title; ?>"><?php echo $post_title; ?></a></h4>
                            <div class="clear"></div>

                <?php 
                        }
                }
        ?>
但是我想在两个不同的地方使用这个代码,所以我想抵消前3篇文章。我有没有办法抵消这三个职位?谢谢

2 个回复
SO网友:kaiser

这是您的代码包装在插件中。重要的是prepare()s输入,因此查询是安全的。您可以定义两个参数:偏移量和;限度这些基本上就是SQLLIMIT.

如果你对插件没有任何用处,只需停用它,因为它不会做任何事情-它的输出附加到一个过滤器&hellip;

$most_commented = apply_filters( \'wpse70027_display_comments\', 0, 3 );
// Now process the result in your template...
&hellip;它可以简单地放入任何模板文件中。

<?php
/**
 * Plugin Name: (#70027) »kaiser« Get Most Commented. 
 * Description: Just place the filter in your themes template file and you\'re ready to go. 
 */
function wpse70027_get_comments( $offset = 0, $limit = 10 )
{
    global $wpdb;
    static $instance;

    // Validate input
    $offset = absint( $offset );
    $limit  = absint( $limit );
    // Prevent wrong user input
    0 > $offset AND $instance[\'offset\'] = 0;

    // Setup offset/limit as 0/10 on the 1st run
    ! isset( $instance[\'offset\'] ) AND $instance[\'offset\'] = $offset;
    ! isset( $instance[\'limit\'] )  AND $instance[\'limit\']  = $limit;
    // Setup the query string
    ! isset( $instance[\'query\'] )  AND $instance[\'query\']  = 
         "
            SELECT $wpdb->posts.ID, $wpdb->posts.post_title,
                COUNT($wpdb->comments.comment_post_ID) AS cmt
            FROM $wpdb->posts, $wpdb->comments
            WHERE comment_approved  = \'1\' 
                AND $wpdb->posts.ID = $wpdb->comments.comment_post_ID
                AND post_status     = \'publish\' 
                AND comment_status  = \'open\' 
            GROUP BY $wpdb->comments.comment_post_ID
            ORDER BY cmt 
            DESC
            LIMIT %d, %d
         ";
    ! isset( $instance[\'posts\'] )  AND $instance[\'posts\']  = array();

    // Three conditions trigger a new query:
    # A) Plugin is running the first time and \'posts\' isn\'t set
    # B) The input offset is below the default offset
    # C) The input limit is above the default limit
    if (
        ! isset( $instance[\'posts\'] )
        OR ! in_array( $offset, range( $instance[\'offset\'], $instance[\'limit\'] ) )
        OR ! in_array( $limit, range( $instance[\'offset\'], $instance[\'limit\'] ) )
        )
    {
        // Adjust the range
        $instance[\'offset\'] = $instance[\'offset\'] > $offset ? $offset : $instance[\'offset\'];
        $instance[\'limit\']  = $instance[\'limit\']  < $limit  ? $limit  : $instance[\'limit\'];

        $instance[\'posts\'] = $wpdb->get_results( $wpdb->prepare(
             $instance[\'query\']
            ,$instance[\'offset\']
            ,$instance[\'limit\']
        ) );
    }

    // Only return what was requested
    return array_intersect_key(
         $instance[\'posts\']
        ,range( $offset, $limit )
    );
}
add_filter( \'wpse70027_display_comments\', \'wpse70027_get_comments\', 10, 2 );
不,还有no difference 当你把它应用到你的功能中时。php文件。唯一的问题是,这段代码将在主题更新或更改时消失,所以最好将其保留在插件中

SO网友:chap

您可以使用wordpress查询在一个页面中有多个循环,并在第二个循环中偏移计数。

<?php 
                     $args = array(
                        \'posts_per_page\' => 3,
                        \'orderby\' => \'comment_count\'
                        );

                        $the_query = new WP_Query( $args );

?>

    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

     //Put what you want to output here

    <?php 

        endwhile; 

        endif; ?>

<?php wp_reset_postdata(); ?>
然后在第二个查询中对其进行偏移。

<?php 

                   $args = array(
                        \'posts_per_page\' => 3,
                        \'orderby\' => \'comment_count\',
                        \'offset\'  => 3
                        );

                        $the_query = new WP_Query( $args );



  ?>
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

     //Put what you want to output here

    <?php 

        endwhile; 

        endif; ?>

http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

结束

相关推荐