WordPress.org

Forums

Using "the_content" in the sidebar (8 posts)

  1. converting2wp
    Member
    Posted 5 years ago #

    I'd like to show headlines from different categories in the sidebars of different pages.

    This is working like a champ -- I start with

    $newsposts = get_posts('category='.$category.'showposts='.$number_to_post);
    foreach ($newsposts as $post ) {

    the_permalink(), the_title() and the_excerpt() all work as expected.

    However, on one page I'd like to insert the entire contents of a related post in the sidebar, not just the excerpt. But when I use the_content() in place of the_excerpt, what I get is the content of the containing page, not the content of the post.

    What am I missing?

  2. CallUp
    Member
    Posted 5 years ago #

    the_content() has to be within The Loop to work.

    Are you including that same Loop on that other page?

  3. CallUp
    Member
    Posted 5 years ago #

    Never, mind. I didn't read your question correctly. I see what you're saying now, but am not sure why it's doing that.

  4. converting2wp
    Member
    Posted 5 years ago #

    Thanks, though.

    The only workaround I've come up with is to copy the HTML of the page to the excerpt, but that seems like a kludge (and I haven't actually tested it).

  5. converting2wp
    Member
    Posted 5 years ago #

    The kludge does work, but I'm still wondering if anyone has a better suggestion.

  6. Jonathan Dingman
    Member
    Posted 5 years ago #

    Try putting <?php rewind_posts(); ?> right before the loop in the sidebar, that may fix it.

  7. converting2wp
    Member
    Posted 5 years ago #

    Thanks!

    Putting rewind_posts before or after the get_posts call didn't help.

    Your comment made me dig further and on the Codex page about the Loop, there are some examples of Multiple Loops using alternatives to get_posts: http://codex.wordpress.org/The_Loop#Multiple_Loops

    Things aren't working yet (and it still feels like a bug in get_posts that I may report), but I'll post here as I make progress.

  8. converting2wp
    Member
    Posted 5 years ago #

    This code is working in the sidebar to set up a loop to show different numbers of posts from different categories on different pages' sidebars. [Logic at the top of sidebar.php sets the variables $category, $number_to_post, and others.] The template tags, including the_content(), behave as expected in the "/* do stuff */" block.

    query_posts('cat='.$category.'&showposts='.$number_to_post);
    while (have_posts()) {
    	the_post();
    /* do stuff */
    }

    At the moment, I can't reproduce the original problem with get_posts() and the_content(). Note, however, that query_posts and get_posts do have slightly different lists of legal parameters.

Topic Closed

This topic has been closed to new replies.

About this Topic