0

I have a custom post type and the content looks something like this:

<h3>Description</h3>

The Production Assistant works closely with the production team to ensure that our games ship on time and at our very high standards. You will be a part of an international team and gain valuable insight into game development from a management perspective. Aside from production, you will also expand your knowledge of different disciplines such as, engineering, design, art, human resources, administration, and information technology.

The problem now is that the excerpt outputs something like this now:

<p>Description The Production Assistant</p>

How can I tell WordPress to trim the h3s and the text inside them?

2
  • Do you want to remove only the <h3> or all <hx> tags?
    – RRikesh
    Commented May 27, 2013 at 6:47
  • RRikesh Only the h3 (all are OK too). And the content inside the tags as well.
    – wyc
    Commented May 27, 2013 at 7:00

3 Answers 3

2

There's a discussion on WP Support Forum that was related to your question. The pastebin constains the function stated in the other answer.

I've decreased the priority to 5 and it works on my test theme.

add_filter( 'get_the_excerpt', 'wp_strip_header_tags', 5);

If you want to target only <h3> you can use this regex:

$regex = '#(<h([3])[^>]*>)\s?(.*)?\s?(<\/h\2>)#';
2
  • Strange, it worked after changing the priority to 5.
    – wyc
    Commented May 27, 2013 at 7:48
  • I guess that something like wp_strip_all_tags() is being called before and cleans up all tags before reaching your function.
    – RRikesh
    Commented May 27, 2013 at 8:07
4

Try with this by pasting these code inside functions.php file under your current theme.

function wp_strip_header_tags( $excerpt='' ) {
    $raw_excerpt = $excerpt;
    if ( '' == $excerpt ) {
        $excerpt = get_the_content(''); 
        $excerpt = strip_shortcodes( $excerpt );
        $excerpt = apply_filters('the_content', $excerpt);
        $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
    }

    $regex = '#(<h([1-6])[^>]*>)\s?(.*)?\s?(<\/h\2>)#';
    $excerpt = preg_replace($regex,'', $excerpt);

    $excerpt_length = apply_filters('excerpt_length', 55);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );

    return apply_filters('wp_trim_excerpt', preg_replace($regex,'', $excerpt), $raw_excerpt);
}
add_filter( 'get_the_excerpt', 'wp_strip_header_tags', 99);
5
  • This will remove all <h1> to <h6>.
    – RRikesh
    Commented May 27, 2013 at 6:20
  • @Subharanjan Thanks, but strange, I still seeing Description The Production...Is the code above removing the content inside the headings as well?
    – wyc
    Commented May 27, 2013 at 6:22
  • @Subharanjan by the way its the exceprt of a custom post type called Jobs. Not sure if that's the problem.
    – wyc
    Commented May 27, 2013 at 6:26
  • @janoChen: Are you using the_excerpt(); ? Try using get_the_excerpt() function inside loop. Custom post type doesn't matter here. $my_excerpt = get_the_excerpt(); echo $my_excerpt; Commented May 27, 2013 at 7:47
  • @Rikesh: You are correct. It will strip out h1 to h6. This should work as you have mentioned. $regex = '#(<h([3])[^>]*>)\s?(.*)?\s?(<\/h\2>)#'; Commented May 27, 2013 at 7:48
1

This is my solution on https://supersnelgezond.nl and it works like a charm:

function strip_heading_tags( $text ) {
$raw_excerpt = $text;
if ( '' == $text ) {
    //Retrieve the post content.
    $text = get_the_content(''); 
 
    //Strips the header tags and their content.
    $regex = '/<(h[1-6])>.*?<\/\1>/';
    $text = preg_replace($regex,'', $text);
    
    $text_length = apply_filters('text_length', 55);
    $text_more = apply_filters('text_more', '...');
    $text = wp_trim_words( $text, $text_length, $text_more );
    apply_filters('wp_trim_excerpt', preg_replace($regex,'', $text), $raw_excerpt);
    }
    return $text;
}
add_filter( 'get_the_excerpt', 'strip_heading_tags', 5);

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.