[resolved] the_content() does not work (7 posts)

  1. VirusZ
    Posted 9 years ago #


    I am currently developing a personal theme and I have run into some problems with the wordpress functions..

    Here is a part of my code that is giving me trouble:

    $posts = get_posts('numberposts=6&offset=0');
    foreach($posts as $post) :
    <li><a href="<?php the_permalink(); ?>" class="lista"><?php echo substr(get_the_title(), 0, 75); if(strlen(get_the_title()) > 75) echo '...';?></a> --- <?php the_content(); ?></li>
    <?php endforeach; ?>

    The function the_content() only outputs a <br /> in the html source. I have tried replacing the_content() with the_excerpt() and this works. I'm not sure why the_content() doesn't work..
    I hope someone can explain me why this happens.

    Thank you!

  2. maerk
    Posted 9 years ago #

    It seems like the_content can't get at the post's ID. This is usually stored in $id when the page is being viewed. See if echo $id; works at all.

  3. VirusZ
    Posted 9 years ago #

    yes.. you are right
    the echo $id does not return anything..
    can the id be intialised in a certain way?
    I only need to show the latest post in a certain category
    I know the code I posted doesn't do that but I would have modified it somehow if the_content() worked.

  4. maerk
    Posted 9 years ago #

    There's a WP function that gets the ID, I can't remember its name though. It might be get_the_ID, search around on the codex it should turn up.

  5. Joshua Sigar
    Posted 9 years ago #

    There is one initialization function that you need to call inside the loop before calling all other template tags.

    foreach($posts as $post) :
    // the_content(), etc

  6. VirusZ
    Posted 9 years ago #

    Thank you!
    That did the trick.
    The codex isn't organised very good... I somehow missed the setup_postdata() function even though I browsed the codex back and forth..
    Thanks for the solution :D

  7. cosmocanuck
    Posted 9 years ago #

    Just found this solution - saved me from great frustration!

    I'm wondering - shouldn't this page in the codex:


    ...be updated to include that quite essential line of code?

    Would've saved me 15 minutes of grief...

    I'm still pretty new and figured I should ask before just going in there and making a change!

Topic Closed

This topic has been closed to new replies.

About this Topic