0

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.

4
  • Try turning on the debugging mode. It will help you identify the problem. Commented Nov 24, 2011 at 3:49
  • you seem to be closing more divs than you opened - check that all opening/closing divs are matched.
    – Michael
    Commented Nov 24, 2011 at 10:48
  • Is there any way you can get $curretnCat dynamically?
    – Chris_O
    Commented Nov 24, 2011 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
    Commented Nov 27, 2011 at 10:04

1 Answer 1

1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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