custom wordpress post loop – hide iframe content

I am trying to create a custom loop to display my posts in different styling.

In my posts, I have mostly Youtube videos and some text around. In my posts loop I need to display only thumbnail of the featured image of the posts and the text, but I don’t want to display any iframes with my Youtube videos.

I was searching for solutions nad I found this – Display only text to WordPress loop without loosing the text formatting – it should remove iframes and images, which would be great for me, but I have multiple posts loop so I don’t really want to affect the_content() function because it is used in many more different loops in my theme, if you understand my problem. I am not really a programmer so this is tough for me.

edit 1
I have added your code into functions.php but nothing happens. here’s my code in template:

<?php 
    global $post; 
    $args = array('orderby' => 'rand', 'posts_per_page' => 1,); 

    $custom_posts = get_posts($args); foreach($custom_posts as $post) : setup_postdata($post); ?>

    <div class="hpvybirame">
    <div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    <h1><a href="https://wordpress.stackexchange.com/questions/253491/<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

    <?php show_only_thumbnails($content); ?>
    <?php echo get_the_post_thumbnail($thumbnail->ID, 'vlog-lay-c'); ?>
    </div>
    </div>
    <? endforeach; ?>

2 Answers
2

the best solution is to add filter in your functions.php :

add_filter('the_content','show_only_thumbnails',99);

function show_only_thumbnails($content){
    if(is_home() && is_front_page()){
     //quote from :
     //http://wordpress.stackexchange.com/questions/218305/display-only-text-to-wordpress-loop-without-loosing-the-text-formatting/218314#218314
     $content = preg_replace('/<iframe.*?>/', "", $content); // removes iframes
      return $content;
    }
   return $content;
}

Leave a Comment