使用多个循环时搜索小部件会中断吗?

时间:2013-01-24 作者:Dejo Dekic

我已经创建了wordpress模板,它通过WP\\u查询对象使用两个循环。除了搜索小部件外,一切正常。三天来我一直在胡思乱想,现在像个疯子一样在谷歌上搜索,但没有找到解决办法。例如,在我的搜索框中,我键入:第一个cat(即“cat”类别中我的帖子的名称和内容),然后单击搜索,什么都不会发生。

奇怪的是:如果我在其他两个循环的上方创建一个“正常”(主循环),搜索小部件就会工作,但问题是,现在我的“正常”(主循环)会显示其他两个循环中的所有帖子
有人能帮我一下或给我一个建议如何解决这个问题吗
Link to my site 如果要检查我的非工作搜索小部件:(
Index.php)如下所示:

 <?php get_header(); ?>
<?php get_sidebar(\'Main Sidebar\'); ?>   
<div id="blogwrapper">  
<div id="blog">  
    <?php if (have_posts()) : ?>        
    <?php                             
    $paged1 = isset( $_GET[\'paged1\'] ) ? (int) $_GET[\'paged1\'] : 1;
    $paged2 = isset( $_GET[\'paged2\'] ) ? (int) $_GET[\'paged2\'] : 1;
    $args=array(

    \'category_name\' => \'cats\',
    \'paged\' => $paged1,
    \'posts_per_page\' => 2,
    \'order\' => \'DESC\'
     );
     $temp = $wp_query;
     $wp_query= null;
     $wp_query = new WP_Query($args);
     while ( $wp_query->have_posts() ) : $wp_query->the_post();
     ?>     
    <div class="post">  
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 

        <!--< ?php the_meta(); ?>-->

            <?php the_post_thumbnail(); ?>
            <?php the_content(\'Read on...\'); ?>

            <p class="postmetadata">
            <?php _e(\'Filed under&#58;\'); ?> <br/><?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link(\'No Comments &#187;\', \'1 Comment &#187;\', \'% Comments &#187;\'); ?> <?php edit_post_link(\'Edit\', \' &#124; \', \'\'); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn\'t here.</p>
    <?php endif; ?>
    <div class="navigation">
    <?php
     $pag_args1 = array(
        \'format\'  => \'?paged1=%#%\',
        \'current\' => $paged1,       
        \'total\'   => $wp_query->max_num_pages,      
        \'add_args\' => array( \'paged2\' => $paged2 )
    );
    echo paginate_links( $pag_args1 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>
    </div><!--/blog-->          
    <div id="blogs">    
    <?php            
$args2=array(

\'category_name\' => \'dogs\',
\'paged\' => $paged2,
\'posts_per_page\' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args2);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="post">      
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content(\'Read on...\'); ?>
            <p class="postmetadata">
            <?php _e(\'Filed under&#58;\'); ?> <?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link(\'No Comments &#187;\', \'1 Comment &#187;\', \'% Comments &#187;\'); ?> <?php edit_post_link(\'Edit\', \' &#124; \', \'\'); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>       
    <div class="navigation">        
    <?php
       $pag_args2 = array(
        \'format\'  => \'?paged2=%#%\',
        \'current\' => $paged2,
        \'total\'   => $wp_query->max_num_pages,
        \'add_args\' => array( \'paged1\' => $paged1 )
    );
    echo paginate_links( $pag_args2 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>              
    </div>
     </div><!--blogswrapper-->
   <?php get_footer(); ?>
修改索引。php和主循环都可以运行(主循环显示其他两个循环中的所有帖子,但搜索小部件可以运行。)

 <?php get_header(); ?>
<?php get_sidebar(\'Main Sidebar\'); ?>   
<div id="blogwrapper"> 
    <div id="blog">
       //Now Search Widget is WORKING but its\'s showing posts from 
       //other TWO LOOPS so it looks like a BIG mess:( 
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

    <div class="post">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content(); ?>

            <p class="postmetadata">
            <?php _e(\'Filed under&#58;\'); ?> <?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link(\'No Comments &#187;\', \'1 Comment &#187;\', \'% Comments &#187;\'); ?> <?php edit_post_link(\'Edit\', \' &#124; \', \'\'); ?>
            </p>

        </div>
    </div>

    <?php endwhile; ?>

    <div class="navigation">
    <?php posts_nav_link(); ?>
    </div>

    <?php endif; ?>
</div>  
<div id="blog">  
    <?php if (have_posts()) : ?>        
    <?php                             
    $paged1 = isset( $_GET[\'paged1\'] ) ? (int) $_GET[\'paged1\'] : 1;
    $paged2 = isset( $_GET[\'paged2\'] ) ? (int) $_GET[\'paged2\'] : 1;
    $args=array(

    \'category_name\' => \'cats\',
    \'paged\' => $paged1,
    \'posts_per_page\' => 2,
    \'order\' => \'DESC\'
     );
     $temp = $wp_query;
     $wp_query= null;
     $wp_query = new WP_Query($args);
     while ( $wp_query->have_posts() ) : $wp_query->the_post();
     ?>     
    <div class="post">  
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 

        <!--< ?php the_meta(); ?>-->

            <?php the_post_thumbnail(); ?>
            <?php the_content(\'Read on...\'); ?>

            <p class="postmetadata">
            <?php _e(\'Filed under&#58;\'); ?> <br/><?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link(\'No Comments &#187;\', \'1 Comment &#187;\', \'% Comments &#187;\'); ?> <?php edit_post_link(\'Edit\', \' &#124; \', \'\'); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>
    <?php else : ?>
    <h2>Not Found</h2>
    <p>Sorry, but you are looking for something that isn\'t here.</p>
    <?php endif; ?>
    <div class="navigation">
    <?php
     $pag_args1 = array(
        \'format\'  => \'?paged1=%#%\',
        \'current\' => $paged1,       
        \'total\'   => $wp_query->max_num_pages,      
        \'add_args\' => array( \'paged2\' => $paged2 )
    );
    echo paginate_links( $pag_args1 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>
    </div><!--/blog-->          
    <div id="blogs">    
    <?php            
$args2=array(

\'category_name\' => \'dogs\',
\'paged\' => $paged2,
\'posts_per_page\' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args2);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="post">      
    <div class="post_title">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    </div>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content(\'Read on...\'); ?>
            <p class="postmetadata">
            <?php _e(\'Filed under&#58;\'); ?> <?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link(\'No Comments &#187;\', \'1 Comment &#187;\', \'% Comments &#187;\'); ?> <?php edit_post_link(\'Edit\', \' &#124; \', \'\'); ?>
            </p>
        </div>          
    </div>
    <?php endwhile;?>       
    <div class="navigation">        
    <?php
       $pag_args2 = array(
        \'format\'  => \'?paged2=%#%\',
        \'current\' => $paged2,
        \'total\'   => $wp_query->max_num_pages,
        \'add_args\' => array( \'paged1\' => $paged1 )
    );
    echo paginate_links( $pag_args2 );
    ?>
    </div>      
    <?php
$wp_query = null;
$wp_query = $temp;
wp_reset_postdata();
?>              
    </div>
     </div><!--blogswrapper-->
   <?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:Dejo Dekic 整理而成

谁来杀了我吧。。。由于这是我开发的第一个主题,我不知道我需要搜索。php让我的表单工作!无论如何,我创建了搜索。php并插入循环,之后我将该文件复制到主题中,我的搜索表单工作得很好
希望这能帮助其他人:)
我的搜索。php如下所示:

<?php
/**
* The template for displaying Search Results pages.
*
* 
* 
* 
*/
    get_header(); ?>
<div id="blog">
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    <div class="post">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="entry"> 
            <?php the_post_thumbnail(); ?>
            <?php the_content(); ?>
            <p class="postmetadata">
            <?php _e(\'Filed under&#58;\'); ?> <?php the_category(\', \') ?> <?php _e(\'by\'); ?> <?php  the_author(); ?><br />
            <?php comments_popup_link(\'No Comments &#187;\', \'1 Comment &#187;\', \'% Comments &#187;\'); ?> <?php edit_post_link(\'Edit\', \' &#124; \', \'\'); ?>
            </p>
        </div>
    </div>
    <?php endwhile; ?>
    <div class="navigation">
    <?php posts_nav_link(); ?>
    </div>
    <?php endif; ?>
    </div>

结束