【问题标题】:wordpress child theme add to the_content overwriting insteadwordpress 子主题添加到 the_content 覆盖而不是
【发布时间】:2015-04-24 15:32:18
【问题描述】:

我使用 wordpress 2013 模板创建了一个子主题。

我想在 the_content() 之后添加一些内容,所以我做了一个这样的过滤器:

function bluebaronhomepage(){   
    $content .= '<h1>hello from extra content</h1>';
    return $content;
}....

add_filter('the_content', 'bluebaronhomepage');

这将覆盖页面中的内容并仅显示“你好....”。我希望它会在最后附加 hello 的内容

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您的过滤器应采用$content 作为参数:

    function bluebaronhomepage($content){   
        $content .= '<h1>hello from extra content</h1>';
        return $content;
    }
    
    add_filter('the_content', 'bluebaronhomepage');
    

    在您发布的代码中,$content 未定义,然后您将其设置为 &lt;h1&gt;...。当您返回该字符串时,您会覆盖所有内容。

    【讨论】:

      【解决方案2】:

      您应该通过参数将内容传递给您的过滤器:

      function bluebaronhomepage($content = ''){   
        $content .= '<h1>hello from extra content</h1>';
        return $content;
      }....
      
      add_filter('the_content', 'bluebaronhomepage');
      

      您可以在WordPress Codex 获得有关此过滤器的更多信息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-02
        • 2019-02-28
        • 2012-06-01
        • 1970-01-01
        • 2013-11-10
        • 2015-08-09
        • 1970-01-01
        • 2015-09-15
        相关资源
        最近更新 更多