【问题标题】:How to display only specific part of the_content(); text?如何仅显示 the_content() 的特定部分;文本?
【发布时间】:2013-05-08 20:51:48
【问题描述】:

有什么方法可以修剪内容并仅在 wordpress 中的 h2 标签内显示文本?

我试图在我的特定页面上仅显示帖子内容的特定部分,而不是全部。摘录在这种情况下不起作用,因为它仅由长度定义。我敢打赌有一种简单的方法可以做到这一点......只是还找不到......

谢谢大家.....

【问题讨论】:

    标签: wordpress tags


    【解决方案1】:

    这可以通过多种方式实现,但最简单的是:

    function o99_filter_the_content_h2( $content ) {
    
    
     $pattern = "/<h2>(.*?)<\/h2>/";
        preg_match($pattern, $content, $matches);
        return $matches[1];
    }
    
    add_filter( 'the_content','o99_filter_the_content' );
    

    将其添加到您主题中的functions.php 文件中。

    请注意,它将不再是 h2 :-)

    如果您想对任何其他标签使用相同的标签,您可以使用更通用的功能:

     function o99_filter_tags_generic($content, $tagname)
     {
        $pattern = "/<$tagname>(.*?)<\/$tagname>/";
        preg_match($pattern, $string, $matches);
        return $matches[1];
     }
    

    $tagname 是要过滤的标签

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2020-07-03
      • 2011-03-17
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多