Support » Themes and Templates » Show excerpt if available, otherwise show the content

  • Hello,

    I’ve been doing some research about how to set up a conditional statement in a theme (the item.php file for instance) where I can show either the content of the post or an excerpt if it’s available. If there’s no excerpt, I still want the content to appear, so if the ‘more’ tag is used, I still want to see the ‘read more’ link.

    I’m having some trouble understanding which code to use though. I was going to try this:

    <?php
    // Get $post if you're inside a function
    global $post;
    
    if ( empty($post->post_excerpt) ) {
        the_content('Read more...');
    } else {
        the_excerpt();
    }
    ?>

    Would that be correct? Or is there a better way? I’m not sure what the ‘global $post;’ does, would that mess something up? I’ve never used that before.

  • The topic ‘Show excerpt if available, otherwise show the content’ is closed to new replies.