【问题标题】:Wordpress filter get_the_content()Wordpress 过滤器 get_the_content()
【发布时间】:2014-06-11 06:50:28
【问题描述】:

我正在编写一个需要添加内容的插件;向 the_content() 添加过滤器非常简单,但我正在测试的主题使用 get_the_content() 来构建页面。 有迹象表明它应该在 Wordpress 法典中是可能的,但我不明白这个例子:

如果您使用过滤内容的插件 (add_filter('the_content')),那么这将不会应用过滤器,除非您以这种方式调用它(使用 apply_filters):

apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))

谁能帮忙/解释一下?

谢谢,

大卫。

【问题讨论】:

    标签: wordpress filter hook


    【解决方案1】:

    他们的意思是您必须将代码添加到调用get_the_content() 的位置。根据您的描述,这在主题中 - 您必须按照描述更改主题的 get_the_content() 调用。

    原因是get_the_content() 函数中没有过滤器可以与您的插件挂钩(看看the source - 在get_the_content() 中没有对apply_filters() 函数的调用,除了一个“更多”链接文本)。

    【讨论】:

      【解决方案2】:

      派对迟到了,但它开始了:

      在您的 functions.php 中,您可以添加如下内容:

      function do_filter_stuff($content) {
          //Whatever you want to do with your content
      }
      add_filter( 'the_content', 'do_filter_stuff' );
      

      接下来,在您的 page.php(或您想在其中使用它的任何模板)中,您可以使用以下相同的过滤器调用原始内容:

      <?php echo do_filter_stuff(get_the_content()); ?>
      

      【讨论】:

      • 当你直接调用过滤器函数时,为什么要首先添加过滤器?如果您能够更改模板,则根本不需要过滤器。
      • 因为根据 codex:“与 the_content() 的一个重要区别是 get_the_content() 不会通过 the_content 过滤器传递内容。这意味着 get_the_content() 不会自动嵌入视频或除其他外,扩展简码。” developer.wordpress.org/reference/functions/get_the_content
      • 啊,我现在明白了:在主题中使用the_content() 时,使用过滤器。对于所有出现的get_the_content(),您都在手动调用过滤器函数。 (当然,您仍然必须使用子主题,并且也可以替换所有对 the_content() 的调用。恕我直言,这样会更加一致。)
      猜你喜欢
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多