Wordpress:不停止或重置查询

Wordpress:不停止或重置查询,wordpress,while-loop,Wordpress,While Loop,我正在向标题部分添加后缩略图jquery滑块,但这会导致奇怪的问题。不知何故,它并没有在结束或停止查询时结束,所以若我转到单个帖子或页面,它将继续显示循环,而不是页面或帖子内容 我尝试了两个不同的查询,但没有一个停止到这个奇怪的问题 第一次尝试 <?php query_posts( 'post_status=publish&orderby=rand' ); while (have_posts()) : the_post();

我正在向标题部分添加后缩略图jquery滑块,但这会导致奇怪的问题。不知何故,它并没有在结束或停止查询时结束,所以若我转到单个帖子或页面,它将继续显示循环,而不是页面或帖子内容

我尝试了两个不同的查询,但没有一个停止到这个奇怪的问题

第一次尝试

<?php
    query_posts( 'post_status=publish&orderby=rand' );      
    while (have_posts()) : the_post();              

    $title_attr = array(
        'title' => get_the_title(),
        'alt' => get_the_title(),
        'class' => get_the_title(),
    );
    echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
    the_post_thumbnail('thumbnail',$title_attr);
    echo '</a>';

endwhile; ?>
<?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query('post_status=publish&orderby=rand');

    // The Loop
    if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();

    $title_attr = array(
        'title' => get_the_title(),
        'alt' => get_the_title(),
        'class' => get_the_title(),
    );
    echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
    the_post_thumbnail('thumbnail',$title_attr);
    echo '</a>';

endwhile; endif; wp_reset_query();?>

超过第二次尝试的次数

<?php
    query_posts( 'post_status=publish&orderby=rand' );      
    while (have_posts()) : the_post();              

    $title_attr = array(
        'title' => get_the_title(),
        'alt' => get_the_title(),
        'class' => get_the_title(),
    );
    echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
    the_post_thumbnail('thumbnail',$title_attr);
    echo '</a>';

endwhile; ?>
<?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query('post_status=publish&orderby=rand');

    // The Loop
    if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();

    $title_attr = array(
        'title' => get_the_title(),
        'alt' => get_the_title(),
        'class' => get_the_title(),
    );
    echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
    the_post_thumbnail('thumbnail',$title_attr);
    echo '</a>';

endwhile; endif; wp_reset_query();?>

所有这些都不会停止在单个帖子或页面中显示循环(所有类似帖子的索引页面)

我会去单一的文章或网页,它是保持显示循环,而不是网页或文章内容

这是因为您没有向它传递任何限制查询单个帖子的参数。您的查询(
$wp\u query->query('post\u status=publish&orderby=rand');
)一直以随机顺序拉取所有帖子。对于单柱显示,您需要传递它。您可能需要使用来检查“p”、“page_id”或两者。大概是这样的:

  $pid = get_query_var('p');
  if (!empty($pid)) {
    $qry = 'p='.$pid;
  } else { 
    $qry = 'post_status=publish&orderby=rand';
  }
  $wp_query->query($qry);
还有其他可能的解决方案,如
is\u single()

此外,WordPress使用变量
$wp\u query
,因此您应该选择另一个变量,而不是重击该变量。

我找到了解决方案:)

我在while之前添加了if(have_posts()),并使用wp_reset_query()结束循环,现在一切都好了。:)

如果有人有同样的问题,这里是最后的代码

<?php
    query_posts( 'post_status=publish&orderby=rand' );      
    if ( have_posts()): while (have_posts()) : the_post();          

    $title_attr = array(
        'title' => get_the_title(),
        'alt' => get_the_title(),
        'class' => get_the_title(),
    );
    echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
    the_post_thumbnail('thumbnail',$title_attr);
    echo '</a>';

endwhile; endif; wp_reset_query(); ?>


感谢您的回复,我的问题是它以我想要的方式显示,但它影响到single.php或page.php应该显示内容的单个帖子和页面,但它是以上述循环格式显示的。对不起。我不理解你的澄清,这听起来一点也不像你最初描述的问题,似乎这不是问题所在。我发现解决方案将为其他用户发布。。谢谢你的帮助