WordPress.org

Support

Support » How-To and Troubleshooting » Conditionally display text using ‘php if’

Conditionally display text using ‘php if’

  • I currently use this piece of code to display the title, excerpt and number of comments from a post:

    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    <?php
    if( $post->post_excerpt ) {
        the_excerpt(); comments_number('(0)', '(1)', '(%)');
    } else {
    }
    ?>
    </li>
    <?php endwhile; ?>

    This produces the following for articles that have excerpts:

    Article title
    Article excerpt (# comments)

    And this if they don’t:

    Article title

    I would like to edit the code so it produces the following for articles that have excerpts:

    Article title – Article excerpt (# comments)

    And this if they don’t:

    Article title

    Presumably in order to do this I have to incorporate the text string ” – ” at some point in the if( $post->post_excerpt ) part of the code. But I don’t know how and all my efforts to do it have produced PHP errors.

    I’m sure someone with a basic knowledge of PHP could solve this in a second. Any takers?

    Thanks in advance…

  • The topic ‘Conditionally display text using ‘php if’’ is closed to new replies.
Skip to toolbar