Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Listing Category Posts Interferes with Static Page Content WordPress

  • SOLVED

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>


[[LINK href="http://iforce.co.nz/i/iaybp3r0.png"]]When there is a post in the category, it <strong>replaces</strong> the original page text.[[/LINK]]

[[LINK href="http://iforce.co.nz/i/djch4i0h.png"]]When there isn't posts in the category[[/LINK]], it shows the <strong>original</strong> 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).

[[LINK href="http://pastebin.com/m75b2fb6f"]]Here's the page source code.[[/LINK]]

Answers (2)

2010-02-07

Julio Protzek answers:

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 comments:

I tried that and the [[LINK href="http://iforce.co.nz/i/bwknbpiw.png"]]content has disappeared entirely[[/LINK]].

Revised source code is [[LINK href="http://pastebin.com/m48aa4ad8"]]here[[/LINK]].


Julio Protzek comments:

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



2010-02-07

neil_pie answers:

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.