2010-07-07 99 views
0

我有一个侧边栏显示在主页面内容之前的代码中。我写了这个(janky)函数来吸引最近的帖子和评论。但是,它很好用,它把我所有的页面都搞砸了,并把所有的帖子都放在了它们上面。如何重置/倒回查询或制作新的查询,以便我的所有网页都显示正确的内容?WordPress的回退查询不起作用

<?php rewind_posts(); ?><?php wp_reset_query(); ?>的arent做的伎俩,我

这里是我的查询:

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

输出评论和帖子 }

+0

“我写了这个(janky)函数来吸引最近的帖子和评论” - 我想你会发现我写了它的伴侣;) – TheDeadMedic 2010-07-07 08:04:40

回答

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()在这里不起作用。请在foreach循环结束后立即致电wp_reset_query()

+0

这很好。感谢您的帮助,我并不是故意说你的代码很笨拙,我的意思是这是WP中最好的方式。我感谢帮助:) – wesbos 2010-07-07 15:12:14

+0

只是在开玩笑 - 这是毫无疑问的,但它可能更加笨拙;) – TheDeadMedic 2010-07-07 15:37:50