WordPress.org

Support

Support » How-To and Troubleshooting » Using “the_content” in the sidebar

Using “the_content” in the sidebar

  • 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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • the_content() has to be within The Loop to work.

    Are you including that same Loop on that other page?

    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.

    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).

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

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

    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.

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Using “the_content” in the sidebar’ is closed to new replies.
Skip to toolbar