Combine wp_trim_words and wp_strip_all_tags


Keywords:wordpress 


Question: 

Can't find the solution of combining wp_trim_words and wp_strip_all_tags inside loop. This is what I have now:

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
       <div class="news-el">
           <a href="">
                <div class="news-content">                      
                    <h3><?php echo get_the_title(); ?></h3>
                    <?php echo wp_strip_all_tags( get_the_content() );?>
                </div>
                <span><b>More...</b></span>
            </a>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

Should I create some additional function?


1 Answer: 

Just nest the functions...

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
       <div class="news-el">
           <a href="">
                <div class="news-content">                      
                    <h3><?php echo get_the_title(); ?></h3>
                    <?php echo wp_trim_words( wp_strip_all_tags( get_the_content(), 55 ) );?>
                </div>
            </a>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

The function will output "..." to the end of the text block of 55 words in this example so you don't really need the span tag. You can customise by specifying a third parameter to the function wp_trim_words as to what you want to appear after the trimmed words.