apply_filters('the_content', $content) with get_posts(); (4 posts)

  1. vehemoth
    Posted 4 years ago #


    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

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

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    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;

  3. vehemoth
    Posted 4 years ago #

    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!

  4. vehemoth
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic