2016-08-21 6 views
3

<div>Ad goes hereの部分がコンテンツの上に表示されるのはなぜですか? $contentの下部に表示されるはずです。私が$contentを直接return $content.'<div>The ads goes here</div>';と返すと、それが下部に表示されます。すべての手がかりは?コールバック関数内で別の関数が呼び出された場合、add_filter( 'the_content')が期待通りに返されません

add_filter('the_content', 'ads_filter'); 

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

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

答えて

3

簡単な解決策は、あなたの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>'; 
} 

あなたがechoを書いて、一度に$contentとそれを連結するとき、それはすでに、外部コンテンツとを出力しますので実際の投稿コンテンツが連結されます。あなたはあなただけの異なる優先順位を変更することにより、それで遊ぶことができますdocumentation

add_filter(string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1) 

からadd_filter関数のシグネチャが表示された場合

add_filter('the_content', 'ads_filter'); 

function ads_filter ($content){ 
    return $content.do_shortcode(ads()); 
} 
add_shortcode('ads_shortcode','here_is_func'); 
function here_is_func(){ 
    return '<div>The ads goes here</div>'; 
} 
function ads(){ 
    return '[ads_shortcode]'; 
} 
+0

ads()関数の戻り値にdo_shortcodeを使用する必要があります。どうすれば可能ですか? –

+0

@JamesHayes私は自分の答えを編集しました –

+0

上記の例で書いたhere_is_func関数は、実際のケースでは少し複雑です。私はむしろ直接エコーする方が好きです。なぜなら、いくつかのjavascriptが埋め込まれているので、それを返すのは難しいからです。私はあなたが理解できることを願っています。 –

2

はここにショートのためにあなたの答えです。下位番号は以前の実行に対応し、同じ優先度を持つ機能はアクションに追加された順に実行されます。単に優先順位をつけて、どこに配置されているかを確認してください。

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

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

function ads(){ 
    echo '<div>The ads goes here</div>'; 
} 
関連する問題