【发布时间】:2013-04-04 22:29:55
【问题描述】:
我正在寻找一种正确的方法来挂钩 Wordpress 中的get_the_content 方法。 Wordpress 为the_content、the_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">→</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">→</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