• Resolved mesalosangeles

    (@mesalosangeles)


    Hi,

    I am trying to insert an in-feed adsense ad into my main content news list.
    I am having trouble getting it to show up correctly. Below is where I am trying to insert the code for adsense. Can someone help me install the code?

    <?php /* Template for displaying posts page and category archives */
    if (have_posts()) :
    	$counter = 1;
    	$max_posts = $wp_query->post_count;
    	while (have_posts()) : the_post();
    		if ($counter == 1) :
    			get_template_part('content', 'lead'); ?>
    			<hr class="mh-separator"><?php
    		else :		
    			get_template_part('content');
    		endif;
    		$counter++;	
    
    	endwhile;
    else :
    	get_template_part('content', 'none');
    endif;
    mh_newsdesk_lite_pagination(); ?>

    Here is my adsense code:

    <script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <ins class=”adsbygoogle”
    style=”display:block”
    data-ad-format=”fluid”
    data-ad-layout=”image-side”
    data-ad-layout-key=”-fk+5y+4w-f5+97″
    data-ad-client=”ca-pub-5823980480550167″
    data-ad-slot=”3785554547″>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi Mesalosangeles,

    thanks for using MH Newsdesk lite.

    It depends on where exactly you want to display the ads. For example if you want to display ads after every story, you could implement the code after:

    get_template_part('content', 'lead');

    and

    get_template_part('content');

    Also keep in mind that the code is PHP and you’re trying to implement HTML. Make sure to end / start the PHP part with ?> and <?php otherwise you’ll have syntax errors which will make your site unaccessible.

    It’s also worth mentioning that you shouldn’t modify core theme files but instead code a custom child theme with your modifications. Otherwise your code modifications will be lost after theme updates.

    Thread Starter mesalosangeles

    (@mesalosangeles)

    Hi Thanks for getting back to me,

    I’m trying to display the ads at random positions 3,4,5,6 on the content list.
    I have tried converting the code to php and placed it under the get_template (content) and still no luck.

    My site is blank from the header down. Here is my code, can someone help fix this issue?

    <?php /* Template for displaying posts page and category archives */
    if (have_posts()) :
    	$counter = 1;
    	$max_posts = $wp_query->post_count;
    	while (have_posts()) : the_post();
    		if ($counter == 1) :
    			get_template_part('content', 'lead'); ?>
    			<hr class="mh-separator"><?php
    		else :		
    			get_template_part('content');
    		
    <? php
    echo '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';
    echo '<ins class="adsbygoogle"';
    echo 'style="display:block"';
    echo 'data-ad-format="fluid"';
    echo 'data-ad-layout="image-side"';
    echo 'data-ad-layout-key="-fk+5y+4w-f5+97"';
    echo 'data-ad-client="ca-pub-5823980480550167"';
    echo 'data-ad-slot="3785554547"></ins>';
    echo '<script>';
    echo '(adsbygoogle = window.adsbygoogle || []).push({});';
    echo '</script>';
    ?>		
    		endif;
    		$counter++;	
    
    	endwhile;
    else :
    	get_template_part('content', 'none');
    endif;
    mh_newsdesk_lite_pagination(); ?>

    Sorry, but we can’t debug custom code here or provide assistance with custom development. This is a free forum to get assistance with the installation, configuration or usage of the theme.

    Also, if you want to display ads for particular posts in the loop only, then you would need to code this accordingly as well by using counters.

    If you’re not familiar with coding and need assistance with custom development, then you could hire a developer or try forums where you can get help with development (e.g. Stack Overflow or else).

    <script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: “ca-pub-2602478736697821”,
    enable_page_level_ads: true
    });
    </script>

    <script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: “ca-pub-9838782138139993”,
    enable_page_level_ads: true
    });
    </script>

    • This reply was modified 6 years, 8 months ago by albin01.
    • This reply was modified 6 years, 8 months ago by albin01.
    • This reply was modified 6 years, 8 months ago by albin01.

    Hi, sorry for jumping in here…..I also have a problem with inserting an Adsense code….I want to place it in the child theme in the content.php ….one before the content and one after. How do I do that? Where exactly should I place it? If I just copy it, it doesn’t work. I would have sent a screenshot, but don’t know how ?? Thank you.

    @passionspoon: Please open your own thread in the support forum of the particluar WordPress theme you’re using.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adsense code for in-feed Ads’ is closed to new replies.