您可以通过添加instant_articles_transformed_element
过滤器来执行此操作,以便相应地修改标头。
这通常用于放置Facebook Audience Network单元,但如果您的手动代码正常工作,则下面的代码应该可以工作,尽管您可能需要使用查询变量。加入的functions.php如下:
在functions.php的顶部,补充一点:
use Facebook\InstantArticles\Elements\Ad;
然后:
/**
* Adds multiple units to the Instant Article
*
* @param Instant_Articles_Post $article
*
* @return Instant_Articles_Post
*/
add_filter('instant_articles_transformed_element', function ($article) {
// Create the base ad
$ad = Ad::create()
->withWidth(300)
->withHeight(250)
->enableDefaultForReuse();
// Retrieve the header
$article->getHeader()
// Add the first ad
->addAd(
$ad->withSource(
// This creates the URL https://www.mywebsite.com/ss;adtype=banner300x250;adslot=1
add_query_arg(
array(
'adtype' => 'banner300x250',
'adSlot' => '1',
),
'https://www.mywebsite.com/ss'
)
)
)
// Add the second ad
->addAd(
$ad->withSource(
// This creates the URL https://www.mywebsite.com/ss;adtype=banner300x250;adslot=2
add_query_arg(
array(
'adtype' => 'banner300x250',
'adSlot' => '2',
),
'https://www.mywebsite.com/ss'
)
)
)
// Add the third ad
->addAd(
$ad->withSource(
// This creates the URL https://www.mywebsite.com/ss;adtype=banner300x250;adslot=3
add_query_arg(
array(
'adtype' => 'banner300x250',
'adSlot' => '3',
),
'https://www.mywebsite.com/ss'
)
)
);
return $article;
});
与该代码,插件将采取照顾其余部分,它会自动将以下代码添加到头部部分:
<meta property="fb:use_automatic_ad_placement" content="enable=true ad_density=default"/>
它还将关闭头前添加下列权利:
<section class="op-ad-template">
<figure class="op-ad op-ad-default">
<iframe src="https://www.mywebsite.com/ss?adtype=banner300x250&adSlot=1" width="300" height="250"></iframe>
</figure>
<figure class="op-ad">
<iframe src="https://www.mywebsite.com/ss?adtype=banner300x250&adSlot=2" width="300" height="250"></iframe>
</figure>
<figure class="op-ad">
<iframe src="https://www.mywebsite.com/ss?adtype=banner300x250&adSlot=3" width="300" height="250"></iframe>
</figure>
</section>
它显示我在空白页上我的网站和帖子的Facebook即时条选项的前端都只是显示加载图标。我看不到调试信息或其他任何东西。 –
刚编辑我的答案...你需要在functions.php的顶部添加这个:使用Facebook \ InstantArticles \ Elements \ Ad; – luqita
太棒了!现在它的工作。你能解释我这个代码吗?请稍微:)并谢谢你! –