0

I've followed the guide below, but have made a few edits to it and now i need some help:

Add Adsense code in index.php

I removed the following line from my code if ( ! is_front_page() ) return; and for the most part it works as intended, however i do have one problem.

I use the plugin "random posts thumbnail" on my site.I also use a snippet of code to inject absence modules in between my posts like so:

 if ( 7 === $count )
        dynamic_sidebar( 'ad_widget_1' );

Thus after every 6 posts it should show an adsense module.

However, for some reason they are being adsense modules are being injected after every 3 posts, which incidentally is right after where my "random posts thumbnails" plugin inserts it's content.

Is it possible that my counting code is including the content generated by the plugin and if so how can i fix it?

    add_action( 'widgets_init', 'wpse_84250_register_ad_widgets' );

    function wpse_84250_register_ad_widgets()
    {
        // used on the first page of main loop only
        register_sidebar(
            array (
                'name'          => 'Ad Widget 1',
                'id'            => 'ad_widget_1',
                'before_widget' => '<div class="frontpage-ads">',
                'after_widget'  => '</div>'
            )
        );
        register_sidebar(
            array (
                'name'          => 'Ad Widget 2',
                'id'            => 'ad_widget_2',
                'before_widget' => '<div class="frontpage-ads">',
                'after_widget'  => '</div>'
            )
        );
    }

add_action( 'loop_start', 'wpse_84250_show_ad_widgets' );

function wpse_84250_show_ad_widgets()
{
    static $count = 0;

    if ( ! is_front_page() )
        return;

    if ( 'loop_start' === current_filter() )
        return add_action( 'the_post', __FUNCTION__ )
            && add_action( 'loop_end', __FUNCTION__ );

    $count += 1;

    if ( 6 === $count )
        dynamic_sidebar( 'ad_widget_1' );

    if ( 11 === $count or 'loop_end' === current_filter() ) {
        dynamic_sidebar( 'ad_widget_2' );
        remove_action( 'the_post', __FUNCTION__ );
    }
}
1
  • Can you please format the question well to increase readability esp the codes. Check the Link for more help Jan 17, 2014 at 6:02

1 Answer 1

0

Bit hard to understand exactly what you want to do as the grammar isn't the best but i assume you want to return false for your Adsense widgets (using the code you linked to) when random posts thumbnail" plugin executes?

You could add a check for the function your random posts thumbnail plugin uses so the widget doesn't execute in that case.

Not sure what plugin you're using though as you haven't linked to it or named it specifically.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.