Wordpress快退查询无法正常工作

时间:2010-07-07 01:40:54

标签: php wordpress

我在主页内容之前的代码中显示了一个侧边栏。我写了这个(janky)函数来引入最近的帖子和评论。然而,它工作得非常好,它与我的所有页面紧密相关,并在所有页面上添加帖子。如何重置/回放查询或创建新查询以使我的所有页面显示正确的内容?

<?php rewind_posts(); ?><?php wp_reset_query(); ?>不是我的伎俩

这是我的问题:

$comments = get_comments('number=10');
$posts    = get_posts('posts_per_page=10&category=6');


$most_recent = array();

foreach ($comments as $comment)
    $most_recent[strtotime($comment->comment_date_gmt)] = $comment;

foreach ($posts as $post)
    $most_recent[strtotime($post->post_date_gmt)] = $post;

unset($comments, $posts);

krsort($most_recent);

$most_recent = array_slice($most_recent, 0, 10);

foreach ($most_recent as $post_or_comment) {

    $is_post  =    isset($post_or_comment->post_date_gmt);
    $comment_id =   $post_or_comment->comment_ID;
  $post_id =    $post_or_comment->ID;  
 $comment_post_id =   $post_or_comment->comment_post_ID; 

 if ($is_post == 1) 

   { ?> <li><a href="<?php  echo get_permalink($post_id); ?>"><?php echo $post_or_comment->post_title; ?></a><span class="tag"><?php echo the_category($post_id); ?></span></li><?php } 

 else 
   { ?><li> <a href="<?php  echo get_permalink($comment_post_id); ?>"><?php echo get_the_title($comment_post_id); ?></a><span class="tag">Comment</span></li><?php }

    // output comments and posts
}

输出评论和帖子        }

1 个答案:

答案 0 :(得分:0)

请尝试使用$my_posts$my_comments,以防WP使用相同命名的全局变量(尽管我认为不是这样)。

此外,在foreach循环中,您应该只在知道对象类型时才转换变量,否则您将访问不存在的属性;

foreach ($most_recent as $post_or_comment) {

    $is_post  =    isset($post_or_comment->post_date_gmt);
    if ($is_post) {
        $post_id =    $post_or_comment->ID;  
    } else {
        $comment_id =   $post_or_comment->comment_ID;
        $comment_post_id =   $post_or_comment->comment_post_ID; 
    }
}

rewind_posts()此处不起作用。只需在wp_reset_query()循环结束后立即致电foreach

相关问题