删除摘录输出中的H3吗?

时间:2013-05-27 作者:janoChen

我有一个自定义的帖子类型,内容如下:

<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.
现在的问题是,摘录现在输出如下内容:

<p>Description The Production Assistant</p>
我如何告诉WordPress修剪H3和其中的文本?

3 个回复
最合适的回答,由SO网友:RRikesh 整理而成

有一个关于WP Support Forum 这与你的问题有关。这个pastebin 约束中所述的函数other answer.

我已将优先级降低为5 它适用于我的测试主题。

add_filter( \'get_the_excerpt\', \'wp_strip_header_tags\', 5);
如果你只想成为目标<h3> 您可以使用此正则表达式:

$regex = \'#(<h([3])[^>]*>)\\s?(.*)?\\s?(<\\/h\\2>)#\';

SO网友:Subharanjan

尝试将这些代码粘贴到函数中。当前主题下的php文件。

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);

SO网友:Carlo

这是我的解决方案https://supersnelgezond.nl 它就像一种魅力:

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);

结束