WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] The_Content() and recent post in category (4 posts)

  1. jfburroway
    Member
    Posted 7 years ago #

    I'm trying to create a single "sticky" for a weekly podcast (postcasts are category 39) at the top of my blog. I want to display the title and the text. I can get the title to show up, but the text (the_content()) is blank.

    The same post shows up in the more conventional loop in the main part of the blog. What's wrong?

    <?php
     $myposts = get_posts('numberposts=1&category=39');
     foreach($myposts as $post) :
     ?>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php the_content(); ?>
     <?php endforeach; ?>

    the_permalink() and the_title() work fine, but the_content() returns nothing.

    test.boxturtlebulletin.com

  2. jfburroway
    Member
    Posted 7 years ago #

    Not resolved yet. ... raising above another accidental duplicate post... Yes, it's one of those days.

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    When you use get_posts(), you don't automatically fill up the internal variables to make the template tags like the_content work properly. You have to call setup_postdata with the post to force that to occur.

    So, change it to this:

    <?php
     $myposts = get_posts('numberposts=1&category=39');
     foreach($myposts as $post) : setup_postdata($post);
     ?>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php the_content(); ?>
     <?php endforeach; ?>
  4. jfburroway
    Member
    Posted 7 years ago #

    You're a genius! And fast too!

    A billion thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic