您可以通过添加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;
});
使用该代码,插件将处理其余部分,它会自动将以下代码添加到head部分:
<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>