Support » Fixing WordPress » Strange the_content() behavior on paginated posts is driving me insane!

  • I’m designing a highly customized WordPress theme with a self-written static sidebar. One of my sidebar blocks shows a single post, in order to display a poll with an image. You can see an example at:

    The poll displayed in the sidebar is a separate loop, written with the following code:

    $polls_cat = "45";
    $poll_query = new WP_Query('cat='.$polls_cat.'&showposts=1&orderby=date&order=DESC');
    while ($poll_query->have_posts()) : $poll_query->the_post(); update_post_caches($posts);?>
    <h2 id="pollboxtitle"><?php the_title(); ?></h2>
    <div id="featuredpicture"> <!-- featured picture wrapper -->
    <img height="200px" width="300px" src="<?php echo get_post_meta($post->ID, 'thumbnail',true); ?>" alt="Post Image" class="postimg" />
    <div class="caption"><?php echo get_post_meta($post->ID, 'thumbnailcaption',true); ?></div>
    </div> <!-- /featuredpicture -->
    <?php the_content(''); ?>
    <?php endwhile; ?>

    When viewing the first page of a paginated post (split into two pages using the <!--nextpage--> tag, the poll displays perfectly. HOWEVER, when viewing the second page of the paginated post, as at the following URL:

    the_content() appends the content of the second page of the post at the end of the poll, causing the post to display twice, once in the main body of the page, and once in the sidebar, underneath the poll.

    I’ve isolated this problem to paginated posts; it doesn’t seem to be happening anywhere else on the site.

    I’m something of a newbie when it comes to WordPress, and a lot of what I’ve done has been done without necessarily knowing whether or not it’s the best way to do it or not. I’m thinking that I’ve done something wrong concerning the_loop(), but doing things like wp_reset_query(); doesn’t seem to have any effect.

    Can anyone give me some advice about how I should be doing this? I’ve searched around and, possibly due to the strangeness of the problem, can’t find any information on it.

    I’m using several plugins to customize my template, including Popularity Contest and a modified version of Lester Chan’s PostViews plugin.

Viewing 9 replies - 1 through 9 (of 9 total)
  • One addendum to this post – I previously stated the the_content() was appending the post’s content; in fact, it is replacing the post’s content – the actual poll content is the voting block area (this is all accomplished using Lester Chan’s wp-polls plugin). The voting block itself is not showing; rather, the post’s content is being output here.

    Change this..




    Hi t31os_ – thanks for the suggestion! However, when I tried changing the code as you suggested, as such:

    $polls_cat = "45";
    $poll_query = new WP_Query('cat='.$polls_cat.'&showposts=1&orderby=date&order=DESC');
    while ($poll_query->have_posts()) : $poll_query->the_post(); setup_postdata($posts); ?>
    <?php the_content('');

    Unfortunately, it didn’t have any effect on the problem – I still see the content of the second page of the paginated post when I try to use the_content() in the sidebar loop query I’ve created. Any other thoughts?

    I’ve done some more Googling and have found some more posts on the topic. It seems that a temporary workaround is to use echo $post->post_content. More information here:

    and a WordPress Trac report here:

    I’m going to test out the workaround now and I’ll post up here for other people who want to use the_content() in the sidebar if it works.

    Well, it worked, and it didn’t. Although it solved the problem, now the block is showing the following:

    [poll id="28"]

    Which is the template tag that WP-Poll uses to display a particular poll. It seems that WP-Polls will only render a poll when it’s accessed through the_content() – which means that I have a problem, since the_content() doesn’t seem to be working in sidebars.

    Anyone have any suggestions, or know anything about this bug? In the WP Trac, it says “Milestone -> 2.8”, but I’m using 2.8 and the bug isn’t fixed.


    There is a .diff patch attached to

    that supposedly fixes the problem by resetting a bunch of globals that WordPress leaves around in wp_reset_query.

    I’ve implemented the patch, and done a wp_reset_query(); both immediately before and immediately after the sidebar loop, as shown here:

    $polls_cat = "45";
    $poll_query = new WP_Query('cat='.$polls_cat.'&showposts=1&orderby=date&order=DESC');
    while ($poll_query->have_posts()) : $poll_query->the_post(); wp_reset_query(); ?>
    <h2 id="pollboxtitle"><?php the_title(); ?></h2>
    <div id="featuredpicture">
    <img height="200px" width="300px" src="<?php echo get_post_meta($post->ID, 'thumbnail',true); ?>" alt="Post Image" class="postimg" />
    <div class="caption"><?php echo get_post_meta($post->ID, 'thumbnailcaption',true); ?></div>
    </div> <!-- /featuredpicture -->
    <?php the_content('');
    <?php endwhile; ?>

    However, the problem is still there. Am I using wp_reset_query() wrong here? I’m using it in single.php as well as in the sidebar, and no matter where I bust it out, I’m always seeing the content of the post in the sidebar.

    So…still not working, and the Trac report has been closed, meaning that it’s apparently been shown to work. What am I doing wrong here?


    I had a similar issue and believe the problem here is a bug within function “setup_postdata()” (found in query.php line 2710 for WP 2.9.1) and its handling of global variables.

    This function sets the global variable “$page” based on the instance variables of the global query object, i.e. “$page = get_query_var(‘page’)”.

    Of course if you have done a separate query (with “get_posts()” or whatever) and are trying to make use of the template functions by calling “setup_postdata()”, this is incorrect as the value of “$page” should be related to your separate query.

    The problem is further compounded because “setup_postdata()” only overwrites the first entry in the global “$pages[]” array if your separate query has only a single page. Hence, the other array entries still contain data from the main page and can be inadvertently accessed by function “get_the_content()” using the incorrect value of “$page”.

    My solution for handling nested loops in this way is as follows:

    $myposts = &get_posts(...) /* Or new WP_Query or whatever. */
    global $post, $page;
    $mysavedpost = $post;
    foreach ($myposts as $post) {
       setup_postdata($post); $page = 1;
       the_title(); the_content(); /* And so on. */
    $post = $mysavedpost; setup_postdata($post);

    But it would still be nice if the problems with “setup_postdata()” were fixed!

    @ mdgl … I know its been a while but just in case you see this again…
    is the “&” before “get_posts()” real or a typo. And if it is real what does it do?

    @safiayonker: in this case, the “&” is probably a typo.

    I’m not a PHP expert, but this is supposed to be how you return a reference rather than a copy of a variable (see the PHP manual for more information).

    At least in WordPress 3.0 it seems that “get_posts” is not declared this way, but “get_children” is (see source file “wp-includes/post.php”), so you should probably use a “&” for the latter but not for the former.

    In practice, I’m not sure how picky the PHP compiler/runtime is about this sort of thing, so you may be able to get away with or without the “&” in many situations.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Strange the_content() behavior on paginated posts is driving me insane!’ is closed to new replies.