Paulund
2014-01-27 #wordpress

Trim Post Content Outside Of Loop

The WordPress loop is used in the theme files to display the post content, when you are inside the loop there are two main methods you can use to display the content. You can either display all the content including all images or you can display the post excerpt which will just return the first 55 works of the content. As the excerpt will return the first 55 words, sometimes this isn't the most informative information of the post. In the edit posts screen you can override this text to be anything you want or change it to another area of the post. To use these functions all you have to do is use the functions the_content() or the_excerpt() in the loop, like the code below.


<?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            
            the_content();
        } // end while
    } // end if
?>

<?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            
            the_excerpt();
        } // end while
    } // end if
?>

The problem comes when you want to use the post excerpt outside of the loop, for example if you use the get_posts() function to get the most recent posts it will return an array of post objects. You can now loop through this array to display each of these post objects one by one. On the WP_Post object you have access to all the properties of the post so you can access the content by using the property post_content.

<?php
$posts = get_posts();

if(!empty($posts))
{
    foreach($posts as $post)
    {
        echo $post->post_content;
    }
}
?>

This will return the same content as the_content() function, but if you want to return the post excerpt you can use the user defined property of post_excerpt.

<?php
$posts = get_posts();

if(!empty($posts))
{
    foreach($posts as $post)
    {
        echo $post->post_excerpt;
    }
}
?>

If the user has not defined a post excerpt then this property will be empty, if you want to simply display the first 55 words of a post you will need to use another function. WordPress has a built in function of wp_trim_words() that allows you to trim content by words.

<?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?>

Now you can simply use this function in your foreach loop if post_excerpt is empty to only display the first 55 words in your content.

<?php
$posts = get_posts();

if(!empty($posts))
{
    foreach($posts as $post)
    {
        if(!empty($post->post_excerpt))
        {
            echo $post->post_excerpt;
        } else {
            echo wp_trim_words($post->post_content);
        }
    }
}
?>

Shortcode In First Words

When you use the the_excerpt() function WordPress will remove the shortcodes from the text and only return words from the content. But when you use the wp_trim_words() function WordPress will just grab the first 55 words in the description even if it's part of a shortcode. This can return part of a shortcode or even a whole shortcode. You don't want to display part of the shortcode to the visitors of your website a useful trick to do is to run the shortcodes in the content before you trim the words. This way you will get the correct output from the shortcodes in the trimmed content area, you do this by using the do_shortcode() function.

$trimmed = wp_trim_words( do_shortcode($post->post_content), 55, '...' );

Settings Up Post Loop

An alternative to using a foreach to loop through all the posts that you return and access the post properties, is to use the WordPress function of setup_postdata() this function will setup the global $post variables which makes it easier when working custom query results so that you can use the normal template tags. This function takes one parameter which is the WP_POST object that you want to use. This means that inside the foreach loop you will be able to use the built in WordPress functions of the_excerpt() and the_content().

<?php
$posts = get_posts();

if(!empty($posts))
{
    foreach($posts as $post)
    {
        setup_postdata( $post );

        the_excerpt();
    }

    wp_reset_postdata(); ?>
}
?>