reidark
reidark

Reputation: 910

WordPress Post Excerpt Class

I'm customizing a WordPress Theme and I am stuck in adding a custom class to the the_excerpt().

I have searched on the web for this function, but nothing is specific to add a custom class.

I've tried the get_the_excerpt(), but this one doesn't work (returns nothing).

Anyone knows how to do this?

Upvotes: 6

Views: 12992

Answers (5)

DeanH
DeanH

Reputation: 523

Thanks to everyone who responded. After reading them all, I tried this, and it works perfectly:

<p>'.get_the_excerpt().'</p>

Note: it does not create an additional paragraph tag or any extra html.

Upvotes: 0

Yassir kensouss
Yassir kensouss

Reputation: 1

you don't have to put the_excerpt() inside a p tag, this is going to generate a new p tag contains the excerpt , all you have to do is to put the_excerpt() inside a div tag with your class name.

<div class="post-sumary">
  <?php the_excerpt() ?>
</div>

Upvotes: 0

Delcio Polanco
Delcio Polanco

Reputation: 422

As far as I'm concern wordpress doesnt provide a solution for this however you can try printing the excerpt using this solutions:

Solution 1: printing the class in the page.

<?php  echo '<p class="yourclass">' . get_the_excerpt() . '</p>' ?>;

Link of where i found answer 1

Solution 2: override excert function:

You can override the excerpt function in wordpress functions.php using the following code and pasting in the same file:

function my_custom_excerpt( $excerpt ) {
           $post_excerpt = '<p class="yourclass">' . $post_excerpt . '</p>';
           return $post_excerpt;
    }
    add_filter( 'the_excerpt', 'my_custom_excerpt', 10, 1 );

Link where i found answer 2

Conclusion: I prefer using the solution #1 because is cleaner and very understable for someone which will maintain the site.

Upvotes: 8

antonio83
antonio83

Reputation: 454

<?php echo  get_the_excerpt(); ?>

Shows the excerpt

<p class="your-class"><?php echo  get_the_excerpt(); ?></p> 

Shows the excerpt in a div with a class

Upvotes: 15

Giovanni Benussi
Giovanni Benussi

Reputation: 3510

You can add this code to your functions.php to add the class myclass to the excerpt container:

add_action('the_excerpt','add_class_to_excerpt');
function add_class_to_excerpt( $excerpt ){
    return '<div class="myclass">'.$excerpt.'</div>';
}

Upvotes: 0

Related Questions