2017-09-26 116 views
0

我想在Facebook即时文章上显示多个广告。我发现这个代码,我尝试手动和它的工作。如何使用wordpress插件在Facebook即时文章中添加多个广告?

<section class="op-ad-template"> 

     <!-- Ads to be automatically placed throughout the article --> 

     <figure class="op-ad"> 
      <iframe src="https://www.mywebsite.com/ss;adtype=banner300x250&adslot=1" height="300" width="250"></iframe> 
     </figure> 

     <figure class="op-ad op-ad-default"> 
      <iframe src="https://www.mywebsite.com/ss;adtype=banner300x250&adslot=2" height="300" width="250"></iframe> 
     </figure> 

     <figure class="op-ad"> 
      <iframe src="https://www.mywebsite.com/ss;adtype=banner300x250&adslot=3" height="300" width="250"></iframe> 
     </figure> 

    </section> 

现在我试图通过Facebook即时文章插件使其成为可能。我没有为这些类型的广告找到任何设置选项。

我试图在谷歌搜索,除了这个无法找到任何东西: https://developers.facebook.com/docs/instant-articles/sdk/transformer-rules

请帮帮我!

A.如何添加多个广告使用FB INSTANT ARTICLE PLUGIN in wordpress?

B.如何添加不同代码使用FB即时文章PLUGIN in wordpress?

回答

0

您可以通过添加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&amp;adSlot=1" width="300" height="250"></iframe> 
       </figure> 
       <figure class="op-ad"> 
       <iframe src="https://www.mywebsite.com/ss?adtype=banner300x250&amp;adSlot=2" width="300" height="250"></iframe> 
       </figure> 
       <figure class="op-ad"> 
       <iframe src="https://www.mywebsite.com/ss?adtype=banner300x250&amp;adSlot=3" width="300" height="250"></iframe> 
       </figure> 
</section> 
+0

它显示我在空白页上我的网站和帖子的Facebook即时条选项的前端都只是显示加载图标。我看不到调试信息或其他任何东西。 –

+1

刚编辑我的答案...你需要在functions.php的顶部添加这个:使用Facebook \ InstantArticles \ Elements \ Ad; – luqita

+0

太棒了!现在它的工作。你能解释我这个代码吗?请稍微:)并谢谢你! –

相关问题