Wordpress 如果在回调函数中调用了另一个函数,则add#filter(';内容';)不会按预期返回

Wordpress 如果在回调函数中调用了另一个函数,则add#filter(';内容';)不会按预期返回,wordpress,Wordpress,为什么广告出现在这里部分显示在内容顶部?它应该显示在$content的底部。如果我直接返回return$content作为return$content.'theads goes here',它显示在底部。有什么线索吗 add_filter( 'the_content', 'ads_filter' ); function ads_filter ($content){ return $content.ads(); } function ads(){ echo '<div&

为什么
广告出现在这里
部分显示在内容顶部?它应该显示在
$content
的底部。如果我直接返回
return$content
作为
return$content.'theads goes here',它显示在底部。有什么线索吗

add_filter( 'the_content', 'ads_filter' );

function ads_filter ($content){
     return $content.ads();
 }

function ads(){
   echo '<div>The ads goes here</div>';
}
add_filter('the_content','ads_filter');
函数ads_过滤器($content){
返回$content.ads();
}
函数ads(){
呼应“广告就在这里”;
}

对于简单的解决方案,请在
ads()函数中使用
return
而不是
echo

add_filter( 'the_content', 'ads_filter' );

function ads_filter ($content){
     return $content.ads();
 }

function ads(){
   return '<div>The ads goes here</div>';
}

如果从中看到
add_filter
函数的签名

你可以通过改变不同的优先级来玩它。较低的数字对应较早的执行,具有相同优先级的函数按照它们添加到操作中的顺序执行。只要给它一个高优先级,看看它放在哪里

add_filter( 'the_content', 'ads_filter', 10 );

function ads_filter ($content){
     return $content.ads();
 }

function ads(){
   echo '<div>The ads goes here</div>';
}
add_filter('the_content','ads_filter',10);
函数ads_过滤器($content){
返回$content.ads();
}
函数ads(){
呼应“广告就在这里”;
}

我需要对ads()函数的返回值使用do\U快捷码。。。怎么可能呢?在我的实际情况中,您在上面的示例中编写的函数是有点复杂。我更喜欢直接回显,因为它很难返回,因为其中嵌入了一些javascript。我希望你能理解。是的,我理解,我们也可以返回javascript,但它看起来很可笑:)
add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
add_filter( 'the_content', 'ads_filter', 10 );

function ads_filter ($content){
     return $content.ads();
 }

function ads(){
   echo '<div>The ads goes here</div>';
}