【问题标题】:Wordpress: How to Hook into get_the_contentWordpress:如何挂钩 get_the_content
【发布时间】:2013-04-04 22:29:55
【问题描述】:

我正在寻找一种正确的方法来挂钩 Wordpress 中的get_the_content 方法。 Wordpress 为the_contentthe_excerpt()get_the_excerpt() 提供挂钩,但不为get_the_content() 提供挂钩。我有一个插件,我维护它在内容帖子中附加和前置一些 HTML。 我对the_excerpt()get_the_excerpt() 使用了相同的方法。我应该怎么做才能支持get_the_content() 以及我看到现在大多数主题都使用get_the_content()?即:如果主题使用get_the_content() 而不是the_content(),我的插件将失败。

我的插件代码如下:

add_action('init', 'my_init');

function my_init() {
  add_filter('the_content', 'my_method', 12);
  add_filter('get_the_content', 'my_method', 12);
}


function my_method($content) {
  $content = "blah blah".$content."blah blah";

  return $content;
}

以上代码(在我的插件中)从主题中监听the_content call,并将我自己的内容添加到文章中。

假设这是主题文件中的方法:

<div class="entry-content">
  <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->

在上述情况下它可以正常工作。

但是如果主题代码是这样的(注意从 the_content() 到 get_the_content()) 的变化。

<div class="entry-content">
  <?php echo get_the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentytwelve' ) ); ?>
</div><!-- .entry-content -->

我的插件失败,因为没有为 get_the_content 定义挂钩。由于我无法控制主题代码,Wordpress 在documentation 中解释的解决方案对我没有帮助:

<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>

更新:为了清晰起见,扩展了问题定义。

【问题讨论】:

    标签: php wordpress plugins filter hook


    【解决方案1】:

    下面的代码可以解决问题(注意my_post 那里):

    add_action('init', 'my_init');
    
    function my_init() {
      add_filter('the_content', 'my_method', 12);
      add_filter('the_excerpt', 'my_method', 12);
      add_filter('the_post', 'my_post');
    }
    
    function my_post($post) {
      $post->post_excerpt = get_the_excerpt();
      $post->post_content = get_the_content();
      return $post;
    }
    
    function my_method($content) {
      $content = "blah blah".$content."blah blah";
      return $content;
    }
    

    【讨论】:

      【解决方案2】:

      您是否尝试过以下方法?

      根据法典:

      如果您使用过滤内容的插件 (add_filter('the_content')), 那么这将不会应用过滤器,除非你这样称呼它 (使用 apply_filters):

      <?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>
      

      它在wordpress codex 中记录。

      已编辑

      尝试使用输出缓冲将the_content 放入变量中。

      这样你就不用担心get_the_content

      ob_start();
      the_content();
      $newContent = ob_get_clean();
      

      现在它存储在一个名为 $newContent 的变量中,您可以轻松操作。

      【讨论】:

      • 我已经检查过了。但这似乎更像是主题开发人员在调用 get_the_content() 方法时必须做的事情。我不确定这是否可以用于我的情况。
      • 您说您使用的是插件,它适用于插件案例,您尝试过吗?
      • 是的,我试过了,但失败了。我猜它适用于想要使用 get_the_content() 方法修改内容而不是松开附加到 the_content 钩子的过滤器的插件。就我而言,我正在查看使用 get_the_content 作为在页面上打印内容的方法的主题,并且我想通过我在插件文件中定义的方法传递内容。如果让您感到困惑,我很抱歉。
      • 我进一步扩展了我的问题定义。希望现在很清楚。
      • 为什么不用输出缓冲区覆盖内容?在 add_filter 中使用输出缓冲区?
      【解决方案3】:

      只需尝试以下方法:

      Cform 示例:

      add_filter('get_the_content', 'cforms_insert',10);
      

      为您的Reference

      (或)

      $content = get_the_content();
      $content = apply_filters('the_content', $content);
      $panels = explode("[newpage]", $content);
      

      我认为这可以帮助您解决问题。

      【讨论】:

      • add_filter('get_the_content', 'my_method',10); 这对我不起作用。他们真的有一个钩子'get_the_content'吗?在documentation 中也找不到它。第二个选项,因为我没有处理主题结尾,我很无奈。如果用户使用没有提及代码的主题,则会失败。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 2018-10-09
      相关资源
      最近更新 更多