2

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="<?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
  • How are you embedding the youtube videos? Are you using the embed shortcode or putting the iframes in your content?
    – Tunji
    Jan 22, 2017 at 13:02
  • I just add the "link" into wordpress post editor and in a second wordpress loads video by itself. You can see it live here: veckotest.8u.cz - under the main big video there's random displayed video and two text blocks on the right side ... If I click with right button on the mouse on the displayed video and open the firebug, I can see in the code "iframe" ... but when I display the code of the page, then I don't see this block with video and text content at all ... now I am confused Jan 22, 2017 at 14:24

2 Answers 2

0

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;
}
8
  • Thanks for your reply. I can understand what this code does, it looks good, but I didn't mention that this my loop is displayed on page, where are more post loops, if you understand what I mean, so I guess that even other loops which also use "the_content" will be affected by this filther? right? If that's true then this is not the best solution for my situation ... sorry for mentioning this in second comment, I should have mention this information above... Jan 22, 2017 at 10:08
  • I guess I need some code (function?) to do exactly the same thing as the_content() but with this preg_replace special line of code from this question: wordpress.stackexchange.com/questions/218305/… - but I don't really want to affect the_content() function because it is used on the same page in other loops too Jan 22, 2017 at 10:12
  • you can try using add_post_meta to the post to reference the tags or iframes or thumbnails and in functions.php you can decide whether to show or hide or replace , if this is not helping try posting a full snippet of the code
    – sdx11
    Jan 22, 2017 at 10:43
  • I am trying to display "random post" on homepage with it's own loop. All my posts contain one paragraph of text and one youtube video. In my loop on homepage I want to display the paragraph of text - so I am using the_content() to do that but it also displays the youtube video. I want to hide the youtube video so I need to "filter out" the iframe code. I have also other loops on my homepage that use "the_content()" and showing the youtube video and I want to keep it that way, so I think that I need to create maybe the_content2() function that filters out iframe - It would work perfectly for me Jan 22, 2017 at 11:11
  • But I am not a programmer and I don't reall know how to write this in code :-/ Jan 22, 2017 at 11:13
0

I believe you should be able to achieve this using the is_main_query function and the_content filter.

The is_main_query checks if the current query is the main or secondary query in the loop.

function wpse253491_strip( $content ) {
    if ( is_home() || is_front_page() && !is_main_query() ) {
        $content = preg_replace('#<\s*iframe[^>]*>.*?<\s*/\s*iframe>#msi', '', $content);
    }
    return $content;
}

add_filter( 'the_content', 'wpdocs_remove_shortcode_from_index' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.