Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Style the_excerpt if exists hide if not WordPress

  • SOLVED

I am trying to add a piece of code to my theme that wraps the <?php the_excerpt();?> code in a IF statement so that I can style within the statement.

For example I want something like

<li>*if statement <span class="spacing"><?php the_excerpt();?></span> end if statement</li>

If there's content then it shows with my custom span class
If there's no content then it leaves it blank <li></li>

Answers (2)

2020-05-12

Arief Fajar Nursyamsu answers:


<li><?php if(has_excerpt()){ ?><span class="spacing"><?php the_excerpt();?></span><?php }?></li>


twdesigns comments:

For some reason this doesn't work. If I use <span class="spacing"><?php the_excerpt();?></span> it shows content but if I use the full code above nothing shows up.... is there a way to pull information from the_content as the excerpt? That may be my problem

2020-05-12

Hariprasad Vijayan answers:

Hi there,
You can try this,

<li>
<?php if ( has_excerpt() ) : ?>
<span class="spacing"><?php the_excerpt();?></span>
<?php endif; ?>
</li>


Kind Regards,
Hariprasad