0

我已经尝试了以下链接上的解决方案,但是,我无法让它们显示帖子!

http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
http://wordpress.stackexchange.com/questions/1529/displaying-posts-depending-on-number-of-comments-and-latest-comments
http://wordpress.org/support/topic/sort-by-number-of-comments

这是我的默认代码,我的问题是,如何让此代码按评论计数排序?

<?php
/*
Template Name: Homepage
*/
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level; 

$referrer = $_SERVER['HTTP_REFERER'];

?>

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


    <!-- BEGIN CONTENT -->  

    <div id="content">


        <!-- BEGIN POST CONTENT --> 

        <?php if($post->post_content) { ?>  

            <div id="post-content">
                <?php the_content(__('Read More &raquo;', 'gp_lang')); ?>
            </div>

            <?php } ?>

        <!-- END POST CONTENT -->           


    </div>

    <!-- END CONTENT -->    


<?php endwhile; endif; ?>


<?php get_footer(); ?>

这是我尝试过但没有成功的代码示例

<?php
/*
Template Name: Homepage
*/
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level; 

$referrer = $_SERVER['HTTP_REFERER'];

?>
 <?php

 $querystr = "
SELECT $wpdb->posts.* 
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
AND $wpdb->postmeta.meta_key = 'tag' 
AND $wpdb->postmeta.meta_value = 'email' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT);

 ?>
 <?php if ($pageposts): ?>
 <?php global $post; ?>
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

    <!-- BEGIN CONTENT -->  

    <div id="content">


    <!-- BEGIN POST CONTENT --> 

    <?php if($post->post_content) { ?>  

        <div id="post-content">
            <?php the_content(__('Read More &raquo;', 'gp_lang')); ?>
        </div>

    <?php } ?>

    <!-- END POST CONTENT -->           


</div>

<!-- END CONTENT -->    

 <?php endforeach; ?>
 <?php endif; ?>

<?php get_footer(); ?>
4

1 回答 1

0

经验法则:如果您对默认的 Wordpress 表运行 SQL 查询,您可能做错了。

假设您页面的原始查询中出现的帖子是正确的,并且您只想修改它们的顺序,您可以使用主循环的原始参数来构造一个新的查询并通过它循环:

<?php
/*
Template Name: Homepage
*/
get_header(); global $gp_settings, $user_ID, $user_identity, $user_level; 
$referrer = $_SERVER['HTTP_REFERER'];

global $wp_query; //Gets the default query
$default_args = $wp_query->query; //Grabs the original arguments
/*
If you find that the above line returns a string rather than an array,
use this instead:
*/
//parse_str($wp_query->query, $default_args);
$default_args['orderby'] = 'comment_count'; //Set the new order parameter
$q = new WP_Query($default_args); //Generate a new Query

if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); //Loop through new Query ?>
    <!-- BEGIN CONTENT -->  
    <div id="content">
        <!-- BEGIN POST CONTENT --> 
        <?php if($post->post_content) { ?>  
            <div id="post-content">
                <?php the_content(__('Read More &raquo;', 'gp_lang')); ?>
            </div>
            <?php } ?>
        <!-- END POST CONTENT -->           
    </div>
    <!-- END CONTENT -->
<?php
endwhile; endif;
get_footer();
?>
于 2013-02-16T04:58:12.530 回答