根据作者的帖子在两个AdSense帐户之间自动切换

时间:2020-04-26 作者:moustafa atiya

我们是一个网站的两名经理,我们每个人都有一个AdSense帐户

基于作者slug或id,我们的广告代码能在我们写的文章中显示吗?

我试图在head部分使用此代码,但没有成功:

<?php if (is_author(\'author 1\');) : ?> <script data-ad-client="ca-pub-ءءءءء1ءءءء" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>; <?php else : ?> <script data-ad-client="ca-pub-ءءءءء2ءءءء" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <?php endif; ?>

任何在账户之间自动切换的想法,而不是作为收入分成。

2 个回复
SO网友:Tony Djukic

因此,我将其分为三部分,如果出现这种情况,可以通过添加其他作者来提供更大的灵活性。

enqueue_adsense() 为adsense添加Google脚本。

add_inline_adsense() 打印adsense代码,以便在页面顶部立即显示广告,因此就在正文标记之后。

function_js_async_attr() 需要将异步代码添加到脚本中。您必须更新要排除的脚本列表,因为我不知道您正在运行哪些脚本。

function enqueue_adsense() {
    wp_enqueue_script( \'google-adsense\', \'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\', array(), \'\', false );
}
add_action( \'wp_enqueue_scripts\', \'enqueue_adsense\', 2 );

function add_inline_adsense() {
    global $post;
    $author_id = $post->post_author;
    //you\'ll want to adjust the author IDs in the following conditions to match what you need
    if( $author_id == 1 ) :
        $data_ad_client     = \'12345678910\';//these need to be changed
        $data_ad_slot       = \'66677666998\';//these need to be changed 
    elseif( $author_id == 2 ) :
        $data_ad_client     = \'01987654321\';//these need to be changed
        $data_ad_slot       = \'66677666998\';//these need to be changed
    endif;
    echo \'<ins class="adsbygoogle" style="display:inline-block;width728px;height:90px; data-ad-client=\' . $data_ad_client . \'" data-ad-slot="\' . $data_ad_slot . \'"></ins>\';
    echo \'<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>\';
}
add_action( \'wp_head\', \'add_inline_adsense\', 3 );

function js_async_attr( $tag ) {
    $scripts_to_exclude = array(); //add a list of scripts that need to be excluded from async - \'script1.js\', \'script2.js\', \'script3.js\'
    foreach( $scripts_to_exclude as $exclude_script ){
        if( true == strpos( $tag, $exclude_script ) ) :
            return $tag;    
        endif;
    }
    return str_replace( \' src\', \' async src\', $tag );
}
add_filter( \'script_loader_tag\', \'js_async_attr\', 102 );
我已经在我目前正在工作的一个网站上测试了这个,它完全按照设计/预期工作。

如果我是为一个长期使用的站点而构建这个站点,我甚至会在用户meta上设置一个adSense ID字段,然后不运行条件,即如果作者是X,请使用adSense ID Y,我只会提取作者提供的adSense ID号,并且只在提供了adSense ID的情况下执行其余功能。虽然我现在没有时间去构建它,也不在您要求的范围内,但我认为这将证明您的项目是可靠的。:-)

SO网友:moustafa atiya

我已经修改了代码以适合我的网站,因为我使用了Jannah WordPress主题

没有明显的错误,但广告没有运行

  <?php
/*
Plugin Name: adsense for author post-
Description: Site specific code changes for my site
*/
/* Start Adding Functions Below this Line */
function enqueue_adsense() {
    wp_enqueue_script( \'google-adsense\', \'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\', array(), \'\', false );
}
add_action( \'wp_enqueue_scripts\', \'enqueue_adsense\', 2 );

function add_inline_adsense() {
    global $post;
    $author_id = $post->post_author;
    //you\'ll want to adjust the author IDs in the following conditions to match what you need
    if( $author_id == 1 ) :
        $data_ad_client     = \'ca-pub-1111111\';//these need to be changed
        $data_ad_slot       = \'111111111111\';//these need to be changed
    elseif( $author_id == 5 ) :
        $data_ad_client     = \'ca-pub-555555\';//these need to be changed
        $data_ad_slot       = \'555555555\';//these need to be changed
    endif;
    echo \'<ins class="adsbygoogle"
     style="display:block"
     data-ad-client=". $data_ad_client ."
     data-ad-slot=". $data_ad_slot ."
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>\';
    echo \'
    <script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>\';
}
add_action( \'wp_head\', \'add_inline_adsense\', 3 );

function js_async_attr( $tag ) {
    $scripts_to_exclude = array(\'advertisement.js\', \'br-news.js\', \'desktop.js\' ,
      \'desktop.min.js\', \'ie.js\', \'live-search.js\' ,\'parallax.js\', \'scripts.js\', \'scripts.min.js\' ,
      \'shortcodes.js\', \'single.js\', \'single.min.js\' ,\'sliders.js\', \'sliders.min.js\', \'velocity.js\' ,
      \'videos-playlist.js\', \'viewport-scripts.js\', \'breakingNews.js\' ,\'fitvids.js\', \'flexMenu.js\', \'ilightbox.dev.js\' ,
      \'lazyload.js\', \'live-search.js\', \'theia-sticky-sidebar.js\' ,\'tiesticky.dev.js\', \'video.dev.js\', \'viewport-checker.js\');
       //add a list of scripts that need to be excluded from async - \'script1.js\', \'script2.js\', \'script3.js\'
    foreach( $scripts_to_exclude as $exclude_script ){
        if( true == strpos( $tag, $exclude_script ) ) :
            return $tag;
        endif;
    }
    return str_replace( \' src\', \' async src\', $tag );
}
add_filter( \'script_loader_tag\', \'js_async_attr\', 102 );


/* Stop Adding Functions Below this Line */
?>