Title: Conditionally display text using &#8216;php if&#8217;
Last modified: August 19, 2016

---

# Conditionally display text using ‘php if’

 *  [f1f](https://wordpress.org/support/users/f1f/)
 * (@f1f)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/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.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [php if](https://wordpress.org/support/topic-tag/php-if/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [f1f](https://wordpress.org/support/users/f1f/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/conditionally-display-text-using-php-if/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
