Forums

[resolved] Getting the_content into a PHP variable... (4 posts)

  1. alexjames01
    Member
    Posted 3 years ago #

    Hey,

    I'm trying to find a way to get the content of a post into a $variable so i can modify the content in the loop (on the fly) using str_replace and then display it.

    The reason for this is that the post content is coming from a rss feed (an image service) and the picture displayed on the post is a thumbnail. I need to change the URL of the IMG tag so it displays the full picture.

    The service has different RSS feeds for thumbnails, medium sized images, and full sized images. All images have similar urls except they end in: -Th.jpg (thumbnail) -Med.jpg, and -Lg.jpg for large images. So using str_replace in variable that holds the content would easily work.

    I am pulling the thumbnail RSS feed which is also being put into a wordpress post. I need it to PULL the thumbnail feed for the thumbnail image on the front page... but when clicked, i would like to have the large image displayed.

    Only way i can think to do this is editing the content within the template file which would mean i need to:

    - Set the post content as a variable
    - use STR_REPLACE to replace -Th.jpg with -Lg.jpg
    - Display the new content

    Any ideas?

  2. NC@WP
    Member
    Posted 3 years ago #

    Use output buffering:

    ob_start();
    the_content();
    $content = ob_get_clean();

    Now you can run str_replace() on $content...

  3. alexjames01
    Member
    Posted 3 years ago #

    Thank you so much! Worked wonderfully!

  4. roblito
    Member
    Posted 3 years ago #

    I had struggled to do a string replace on the category name (add an 's') using the_category() and this worked for me too. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic