Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Is your publication-info block inside the loop? –  Brian Fegter Sep 19 '11 at 16:01
    
I'm assuming so, as $post is populated and get_the_title() works –  djb Sep 19 '11 at 16:11
add comment

1 Answer 1

up vote 6 down vote accepted

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;
share|improve this answer
    
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 Sep 19 '11 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. –  Brian Fegter Sep 19 '11 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 Sep 19 '11 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 Sep 19 '11 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 Sep 17 '13 at 4:35
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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