Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to put a bunch of loops in the side bar using WP_query.

<div id="FL-pest-libray-sidebar">
<?php $category_query_string = new WP_Query(array( 'post_type' => 'page' , 'posts_per_page' => '-1' , 'post_parent' => '413')); if ( have_posts() ) : while ( $category_query_string->have_posts() ) : $category_query_string->the_post(); ?>

<div <?php
     $catspacetitle = get_the_title();
     $cattitle = str_replace(" ", "-", $catspacetitle);

     echo 'id="',$cattitle,'"' 
     ?> class="FL-pest-cat">
<p <?php
     $pcatspacetitle = get_the_title();
     $pcattitle = str_replace(" ", "-", $pcatspacetitle);

     echo 'id="',$pcattitle,'-title"' 
     ?>class="FL-pest-cat-title"><a href="#"><strong>
  <?php the_title(); ?>
  </strong></a></p>


<?php

$postid  = get_the_ID();

if ($postid == '654') {

$curretnCat = '11';}
elseif ($postid == '661') {

$curretnCat = '12';}
elseif ($postid == '662') {

$curretnCat = '13';}
elseif ($postid == '664') {

$curretnCat = '15';}
elseif ($postid == '665') {

$curretnCat = '16';}
elseif ($postid == '666') {

$curretnCat = '17';}
elseif ($postid == '668') {

$curretnCat = '18';}
elseif ($postid == '669') {

$curretnCat = '19';}
elseif ($postid == '670') {

$curretnCat = '20';}
elseif ($postid == '671') {

$curretnCat = '21';}
elseif ($postid == '673') {

$curretnCat = '22';}
elseif ($postid == '674') {

$curretnCat = '23';}

  echo '<div id="pests-cat-',$curretnCat,'"class="FL-pest-indv">';
  echo '<ul>';

$query_string = new WP_Query(array( 'post_type' => 'Pests', 'posts_per_page' => '-1' , 'cat' => $curretnCat )); if ( have_posts() ) : while ( $query_string->have_posts() ) : $query_string->the_post(); ?>
    <li>
    <p <?php
     $pestspacetitle = get_the_title();
     $pesttitlesearch = array(' ', '(', ')', '.');
     $pesttitlereplace = array('-', '', '','');
     $pesttitle = str_replace($pesttitlesearch, $pesttitlereplace, $pestspacetitle);

     echo 'id="',$pesttitle,'"' 
     ?>class="FL-pest-indv-title"><a href="<?php the_permalink(); ?>"><strong>

  <?php the_title(); ?>
  </strong></a></p>
</li>
<?php endwhile; endif; ?>
<?php   wp_reset_query(); ?>
</ul>
</div>  </div>
<?php endwhile; endif; ?>
<?php   wp_reset_query(); ?>
  </div>

It just ends up breaking the page, but works on it's own as a page template.

share|improve this question
    
Try turning on the debugging mode. It will help you identify the problem. –  Rutwick Gangurde Nov 24 '11 at 3:49
    
you seem to be closing more divs than you opened - check that all opening/closing divs are matched. –  Michael Nov 24 '11 at 10:48
    
Is there any way you can get $curretnCat dynamically? –  Chris_O Nov 24 '11 at 14:25
    
Not that I know of, since the page it is referring to is a page with a page template that pulls from the category I associate with it. If I could get the category of the posts from the loop on that page then I could do it dynamically. –  eteich Nov 27 '11 at 10:04
add comment

1 Answer

Since the sidebar was being deployed while inside the loop, the

if ( have_posts() ) :

was what was causing it to break. Here's the fix:

    <div id="FL-pest-libray-sidebar">
      <?php $category_query_string = new WP_Query(array( 'post_type' => 'page' , 'posts_per_page' => '-1' , 'post_parent' => '413')); while ( $category_query_string->have_posts() ) : $category_query_string->the_post(); ?>
      <div <?php
             $catspacetitle = get_the_title();
             $cattitle = str_replace(" ", "-", $catspacetitle);

             echo 'id="',$cattitle,'"' 
             ?> class="FL-pest-cat">
        <p <?php
             $pcatspacetitle = get_the_title();
             $pcattitle = str_replace(" ", "-", $pcatspacetitle);

             echo 'id="',$pcattitle,'-title"' 
             ?>class="FL-pest-cat-title"><a href="#"><strong>
          <?php the_title(); ?>
          </strong></a></p>
        <?php

        $postid  = get_the_ID();

        if ($postid == '654') {

        $curretnCat = '11';}
        elseif ($postid == '661') {

        $curretnCat = '12';}
        elseif ($postid == '662') {

        $curretnCat = '13';}
        elseif ($postid == '664') {

        $curretnCat = '15';}
        elseif ($postid == '665') {

        $curretnCat = '16';}
        elseif ($postid == '666') {

        $curretnCat = '17';}
        elseif ($postid == '668') {

        $curretnCat = '18';}
        elseif ($postid == '669') {

        $curretnCat = '19';}
        elseif ($postid == '670') {

        $curretnCat = '20';}
        elseif ($postid == '671') {

        $curretnCat = '21';}
        elseif ($postid == '673') {

        $curretnCat = '22';}
        elseif ($postid == '674') {

        $curretnCat = '23';}

      echo '<div id="pests-cat-',$curretnCat,'"class="FL-pest-indv">';

        $query_string = new WP_Query(array( 'post_type' => 'Pests', 'posts_per_page' => '-1' , 'cat' => $curretnCat )); while ( $query_string->have_posts() ) : $query_string->the_post(); ?>
        <p <?php
             $pestspacetitle = get_the_title();
             $pesttitlesearch = array(' ', '(', ')', '.');
             $pesttitlereplace = array('-', '', '','');
             $pesttitle = str_replace($pesttitlesearch, $pesttitlereplace, $pestspacetitle);

             echo 'id="',$pesttitle,'"' 
             ?>class="FL-pest-indv-title"><a href="<?php the_permalink(); ?>"><strong>
          <?php the_title(); ?>
          </strong></a></p>
          <?php endwhile; ?>
          <?php wp_reset_query(); ?>
        </div>
      </div>
    <?php endwhile; ?>
    <?php   wp_reset_query(); ?>
    </div>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.