• We are two managers for one website , each of us have an AdSense account

    Can our ad code display in the articles we write, based on the author ID?

    I tried to use this code in head section , but it did not work :

    <?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;  ?>

    any ideas to switch automatically between the accounts

    thanks

    • This topic was modified 4 years, 7 months ago by mrmaistro.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi mrmaistro,

    you might get a critical PHP error using the code above due to the semicolon in the first line of your code.

    Instead of

    <?php if (is_author('author 1');) : ?>

    use

    <?php if (is_author('author 1') ) : ?>

    According to the is_author manual, “User ID, nickname, nicename, or array of such” so if that is the case with your “author 1” then it should work.

    Thread Starter mrmaistro

    (@mrmaistro)

    Thank Thomas M ,
    unfortunately I tried the modified code and it didn’t work

    How about this code?

    <?php
    	
    	 if ( $post->post_author == '1' ) {
    
    	       echo 'adsense code for 1'; 
    
        } elseif ( $post->post_author == '2' ) {
    
            echo 'adsense code for 2'; 
    
        } else {
    
            echo 'adsense code for 1'; 
    
        }?>

    $post->post_author looks good, I just tested this in the single post template and it worked. You can omit the apostrophe from the numbers because the author ID is an integer.

    I also looked at is_author again and learned that it is for archive pages only.

    What I did for testing was just echoing the variable like

    <?php echo $post->post_author; ?>

    This confirmed that it really returns the author ID.

    Thread Starter mrmaistro

    (@mrmaistro)

    Thank you ,, to continue
    Can you suggest a detailed code to switch between the two ad-sense accounts?
    Can it be placed in the <head> section?
    I tried to use the last one but unfortunately it did not work.

    Sorry, I am not able to work on a full version of the code. There are plugins for managing AdSense ads.

    I am also happy to leave the floor and see someone else jumping in here in the thread.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Switch between 2 AdSense accounts based on the author post’ is closed to new replies.