Support » Fixing WordPress » How do I use `the_content` and The Loop tags on sub pages?

  • I’ve got a problem that I think has a very simple solution.

    I run “The Loop” twice on my blog. Once in index.php for the main feed and once in sidebar.php to publish shorter content from a special category. Look here to see what I mean:

    It work fine on the front page. But when I go into the archives – ie clicking on a permalink, commentlink or coming from another site the content output from The Loop and the tag the_contentwon’t show up in sidebar.php. Like this:

    Any ideas on how i could change so that The Loop finds all the post even on sub pages?

Viewing 8 replies - 1 through 8 (of 8 total)
  • What are you doing to initiate the sidebar loop? I assume you’re not just setting up a “default” post loop.

    If you haven’t looked it over, I’d suggest checking out the custom loop examples found in the Codex page covering The Loop, mainly the last one displayed for “Multiple Loops Version 1” :

    I was playing around with those but can’t get it to work. This is the code that I’m using for the loop in sidebar.php

    <?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
    <?php if ( (in_category('3')) ) { ?> <?php //Check if post is in Oddities category ?>

    <li><?php echo $post->post_content; ?></li>
    <?php } ?> <?php // ## Check if post is in Oddities category Ends ## ?>
    <?php endforeach; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Try this:

    <?php $my_query = new WP_Query('cat=3&showposts=10'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><?php echo $post->post_content; ?></li>
    <?php endwhile; ?>

    Adjust the showposts value, depending on how many you want to display.

    Heh, that works like a dream. And, it’s much simpler than what I had done 🙂 Thanks. That’s the second time today!


    I don’t mean to hijack this thread, but since it’s resolved and my problem is very similar so I thought I’d just throw my question in here. I’m using the same code above to selectively insert a # of posts from a certain category. I created a page which I want to function as the index, where I can selectively post the “best of” stuff, and then also have a most recent entries. The problem is, entries display in full, where I want them to have a “Read the rest of this entry.” I’ve looked the_loop and the_content codex stuff with no luck, I can’t get the page to realize it’s a multipost page (I think thats what the problem is). Check out for the broken page and for the index that properly cuts posts at <!–more–>.

    Here’s some details:
    page.php code :
    <?php if ($_GET[’pagename’] = ‘home’) { ?>
    <h2>Our Hottest Content (a good place to start)</h2>
    <?php $hot_query = new WP_Query('cat=8&showposts=4'); ?>
    <?php while ($hot_query->have_posts()) : $hot_query->the_post(); ?>
    <div class="homepost">
    <h2 id="post-<?php the_ID(); ?>">" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></h2>
    <div class="entrytext">
    <?php the_content('Read the rest of this entry »'); ?>
    <?php endwhile;?>
    <?php } else { ?>
    … Thanks for your help!


    Hmmm. I am not a big expert on this, but with some help offered around here I managed something like you want to do just by having a home.php template in the theme. That will take precedence over the index, and I use a similar code BUT without any of those
    <?php if ($_GET[’pagename’] = ‘home’) { ?>...
    just using a code very similar to what Kafkaesquí gave above. (Actually he was the one who helped me out 🙂
    So now when I go to it is the home.php that is showing up (and I don’t have to have /blog/home )
    Maybe I misunderstand what you want, but I thought to share this anyway.

    This is a response to bacon’s question. I have a separate blog page for my site, and I solved this by adding the following in my page template:

    global $more;

    $more = 0;

    Within the get_the_content routine is a check to see if the entire post content should be displayed ($more = 1) or just the portion up to the <!--more--> tag ($more = 0). The $more global is set early on in the Loop in the setup_postdata function.

    You may want to store the current value of $more and then reset it after you’ve run through your custom Loop. That way any other plugin/WP code that checks the variable will work properly.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How do I use `the_content` and The Loop tags on sub pages?’ is closed to new replies.