我正在寻找一种正确的方法来挂钩get_the_contentWordpress 中的方法。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 在文档中解释的解决方案对我没有帮助:
<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>
更新:为清楚起见,扩展了问题定义。