Support » Themes and Templates » apply_filters('the_content', $content) with get_posts();

  • Hi,

    I’m using get_posts() to display posts in a specific manner. I want to get the_content() as a string variable rather than having it display as usual. I’m using the apply_filters() method as described in the the_content() codex but it isn’t working.

    Irrelevant code has been excluded.

    $args = array('numberposts' => 9, 'offset' => 1);
    $posts_array = get_posts( $args );
    
    foreach ($posts_array as $post) : setup_postdata($post);
      the_content(); // returns the content normally
      $content = apply_filters('the_content', $content);
      $content = str_replace(']]>', ']]>', $content);
      echo $content; // returns nothing
    endforeach;

    Thanks in advance for any help I can get with this issue

Viewing 3 replies - 1 through 3 (of 3 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    but it isn’t working

    can you explain this in more detail?

    what are you trying to achieve which you wouldn’t get with using the_content() directly?

    nowhere in your code are you assigning any value to the variable $content.

    possibly try to add (after setup_postdata($post);):

    $content = $post->post_content;

    You hit the nail on the head! That did the trick.

    The reason I’m didn’t want to use the_content() directly is because I needed the content in a string in order to perform other functions prior to it being displayed on the page.

    Thanks again!

    Though I thought that $content = apply_filters('the_content', $content); would have been enough to declare the variable!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘apply_filters('the_content', $content) with get_posts();’ is closed to new replies.