Post emergency WordPress questions for fast help... ...Or answer questions first & win prize money.

$5
Listing Category Posts Interferes with Static Page Content

I have 2 pages (one for each section) that intend to be a 'jumping-off' point to posts in the website. I used the following code to list the recent posts in the category:

<ul class="sidebar">
<?php $recent = new WP_Query("cat=3&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>


When there is a post in the category, it replaces the original page text.

When there isn't posts in the category, it shows the original page text.

What I want to do is have the category listing on the right, but keep the original page text on the left at all times (in this case, the 'Put text here' and whatnot).

Here's the page source code.

Matt Hancock | 02/06/10 at 9:05pm | RSS Answers | Tags

(2) Possible Answers Submitted...

  • avatar
    Last edited:
    02/07/10
    1:27pm
    Julio Protzek says:

    When you call

    $recent->the_post()
    all template tags like
    the_title()
    and
    the_content()
    will be seted to the $recent query.

    To set the template tags to the original text again just call the
    the_post()


    This should solve your problem:
    <div id="right-content"><?php the_post() ?>

    • Matt Hancock says:

      I tried that and the content has disappeared entirely.

      Revised source code is here.

    • Julio Protzek says:

      I didn't spotted that call to

      the_post();
      at line 8. You need to get rid of that.
      Actually get rid of the line 8 entirely. After that you will not need lines 30 and 32, of course.

      Now
      <div id="right-content"><?php the_post(); ?>

      will do the magic.

      Have a nice day :D



  • avatar
    Last edited:
    02/07/10
    1:27pm
    neil_pie says:

    Are you intentionally nesting one loop inside the other? Doing so and calling the_post() will overwrite the values of the outer loop with those of the inner loop, leaving them there even after the inner loop is 'closed'

    from looking at your code, have you tried the following?

    <?php
    /*
    Template Name: New Builds with no Slideshow
    */
    ?>
    <?php get_header(); ?>
    <div class="clear"></div>

    <div id="content">
    <div id="left-content">
    <h2>NEW BUILDS</h2>
    <!--be sure to change cat=4 to whatever category Refits become-->
    <ul class="sidebar">
    <?php
    $stored_query = $wp_query; // saving the natural WP query for the page to that we can return to it after we overwrite the globals with our new query
    $recent = new WP_Query("cat=3&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>

    </div>
    <div id="right-content">
    <?php
    $wp_query = $stored_query; //bring back all the original data for the 'natural' loop
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php $picture = get_post_meta($post->ID, 'picture', true); ?>
    <div id="slide-gallary">
    <img src="<?php echo $picture; ?>" align="left" alt="<?php the_title(); ?>" width="440" height="280" /><br class="clear" />
    </div>
    <div id="colm-3">

    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>


    </div>

    <div class="clear"></div>
    <?php endwhile; ?>


    <?php endif; ?>
    </div>

    </div>
    <div class="clear"></div>


    <?php get_footer(); ?>


    Effectively running your first loop to generate your category lists, then returning to the 'natural' loop for the page content.

    If you are intentionally nesting your custom loop inside the main loop, then calling
    $wp_query = $stored_query;
    before you start messing about, and then
    $stored_query = $wp_query;
    afterwards should reset anything that you have inadvertently tinkered with.

This question has expired.