• cozzy1984

    (@cozzy1984)


    Have tried this on the How-To section, altho was thinking that its more of a hack, so thought I’d try here.

    Is there a hack to exclude images from showing on the main page when using the_content() ?

    I don’t want to use a plugin for this as I’m pretty sure there’s a way to do it. I don’t want to use the_excerpt() as it removes formatting of the post, along with no more link and is set to a certain number of characters. As I would like to use the more tag to select exactly what to show in the post summary.

    Anyway, basically I’m trying to do this as I use the thumbnail option and have a thumbnail showing for the post, but if there’s an image in that start of the post, it shows also. Hence why i want to remove <img> tags from being displayed in when using the_content().

    Really hope someone can help as I’ve been searching for ages.

    Cheers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter cozzy1984

    (@cozzy1984)

    Think i found a solution for anyone who is looking to do the same thing, in excluding images from the_content() for the 1st page or search page without using the_excerpt()

    <?php
       ob_start();
       the_content('Read the full post',true);
       $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
       ob_end_clean();
       echo $postOutput;
    ?>
    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    How is your regex? I gave this a quick search on Topeka (ha, more coffee for me) and tried this on my template this morning.

    Modify your template to use get_the_content() instead of the_content() and then you can play with the output as a string.

    So in your template find in the loop the the_content() put that into $content and then try preg_replace() on that string.

    <?php $content = get_the_content('Read the rest of this entry &raquo;'); ?>
    <?php $content = preg_replace("INSERT-IMG-REGEX-HERE", "", $content); ?>
    <?php echo $content; ?>

    The tricky part is the search and replace regex. You want each <img … /> to be removed but leave the text label only. That’s the tough part…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude images from showing in the_content() ?’ is closed to new replies.