I often find the need to add class or ID's to Wordpress functions. Preferably I would like to do this in the a template (not in functions.php).

Example: <?php the_excerpt(); ?> Will output the excerpt inside <p>. How can I add a class to the paragraph so that I get <p class="something">The excerpt text...</p>

  • WP does not provide filters for this. Your option would be to create your own excerpt function or use $post->post_excerpt instead of the_excerpt() this way you have control on the wrapper and its classes. Sep 23, 2016 at 13:20
  • How do I use $post->post_excerpt? Can you please provide an example?
    – Arete
    Sep 23, 2016 at 13:21
  • e.g. echo '<p>' . $post->post_content . '</p>'; in your template instead of the_excerpt() ; however this will return empty value if the post does not have a set excerpt. Sep 23, 2016 at 13:25
  • Thanks. <?php $the_excerpt = get_the_excerpt(); if ( '' != $the_excerpt ) {} echo $the_excerpt; ?> seemed to do the trick for me. I'll just wrap it in a <p>
    – Arete
    Sep 23, 2016 at 13:31
  • 1
    – bravokeyl
    Sep 23, 2016 at 13:32

2 Answers 2


If you have only one template it's fine to do something like:

echo '<p class="whatever">' . get_the_excerpt() . '</p>';

However, if you have multiple templates and want to control the classes centrally, you can make a filter on get_the_excerpt as follows (but yeah, that would be in functions.php):

add_filter ('get_the_excerpt','wpse240352_filter_excerpt');

function wpse240352_filter_excerpt ($post_excerpt) { 
  $post_excerpt = '<p class="whatever">' . $post_excerpt . '</p>';
  return $post_excerpt;

You would then simply have echo get_the_excerpt(); in your template files.

  • 1
    this may produce an invalid HTML, as you don't know what the excerpt contains, it might for example contain a div which from the spec is not allowed to be in a P Sep 23, 2016 at 15:33
  • ^ post_excerpt, by default, cannot contain HTML. You'd have to modify the edit-post form to allow HTML. So it's pretty reasonable to assume the excerpt is plain text.
    – Slam
    Jan 29, 2018 at 7:44

the way to do this is by wrapping everything in a div with whatever class you want, like

<div class="myexcerpt">
<?php the_excerpt()?>

and then to style it (or JS) you can use

.myexcerpt p {}

div has no semantic value and this kind of things is exactly why it exist.

  • That is exactly what I wanted to avoid; overuse of divs...
    – Arete
    Sep 23, 2016 at 15:27
  • there is no such thing as "overuse of divs" Sep 23, 2016 at 15:31
  • There is definitely a lot of opinions on that subject but, <div class="something"><p></p></div> is not the same as <p class="something"></p> which I requested in the question.
    – Arete
    Sep 23, 2016 at 15:42
  • so the answer is that you can't do what you want. without buffering the output and regex all Ps into that format Sep 23, 2016 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.