8

This code is from my page single-publication.php.

It outputs the relevant custom fields etc (here wrapped in template tags), but the_content() won't output the post content. I've resorted to using $post->post_content (which works), but the mystery remains:

<div class="publication-info">
    <?php printf("<h2>%s</h2>", get_the_title() ); ?>
    <div class="publication-meta publication-credit"><?php the_publication_credit(); ?></div>
    <div class="publication-meta publication-date"><?php the_publication_date(); ?></div><br />
    <div class="publication-blurb" style="font-family:sans-serif;"><?php echo $post->post_content; // the_content() doesn't work. Why not? ?></div>
</div>

What's going on here?

EDIT: I was driven to ask this question because I believed - mistakenly, as it turns out - that $post working and get_the_title() returning a title were an ironclad sign of being inside the loop. But apparently this is not the case. cf Codex on The Loop (second para) and Codex on get_the_title() (parameter list). Can anyone explain?

2
  • 2
    Is your publication-info block inside the loop? Commented Sep 19, 2011 at 16:01
  • I'm assuming so, as $post is populated and get_the_title() works
    – djb
    Commented Sep 19, 2011 at 16:11

2 Answers 2

4

Some post-related data is not available to get_posts by default, such as post content through the_content(), or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument:

<?php
$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
  setup_postdata( $post ); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
<?php endforeach; 
wp_reset_postdata();
?>

See Access all post data

9

There's a good chance your HTML is not included in the loop. Make sure your code looks like this:

if(have_posts()): while(have_posts()): the_post(); ?>

    <div class="publication-info">
        <?php printf("<h2>%s</h2>", get_the_title() ); ?>
        <div class="publication-meta publication-credit"><?php the_publication_credit(); ?></div>
        <div class="publication-meta publication-date"><?php the_publication_date(); ?></div><br />
        <div class="publication-blurb" style="font-family:sans-serif;"><?php echo $post->post_content; // the_content() doesn't work. Why not? ?></div>
    </div>

<?php endwhile; endif;
7
  • yes, this works. thanks. but how could I possibly get_the_title() from outside the loop? the codex says $ID (int) (optional) Post ID or leave empty to use current post in The Loop
    – djb
    Commented Sep 19, 2011 at 16:19
  • Because it uses the $post object that is set for the page. It's a bit wonky for sure. the_content() is dependent on the_post() function. Commented Sep 19, 2011 at 16:30
  • Yes, you have to place the_content() function inside the loop. The way i feel the_content() should be set in a single.php or post-type.php file. But for some reason its not. You still need to have a loop in the single.php or post-type.php file.
    – Sisir
    Commented Sep 19, 2011 at 16:32
  • thanks a lot. a final request: can you point me to any resource that explains a bit more about when and how $post is actually set, if it's not set at the start of the loop? would like to understand.
    – djb
    Commented Sep 19, 2011 at 16:39
  • @BrianFegter correct me if I'm wrong: in single-{post_type}.php, there is always 1 post. Therefore, the while(have_posts()){} can be omitted. Isn't it?
    – Raptor
    Commented Sep 17, 2013 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.