Site logo

Archived topic

how can i insert a ads after 5th post on homepage

28 replies · Started by Rajeev on November 18, 2022

Viewing posts 1–15 of 29

i am just want to know how can i insert a ads after 5th post of homepage ?

Hi there,

Is your homepage the blog page?

Yeah

Hi there,

you can add this PHP Snippet to your site:


function db_hook_inside_shortcode($atts, $content = null) {
      ob_start();
      do_action('db_inside_post_loop');
      return ob_get_clean();
}
add_shortcode('hooky_shortcode', 'db_hook_inside_shortcode');

add_action('generate_after_do_template_part',function(){
    global $loop_counter;
    $loop_counter++;
    if ( $loop_counter == 5 ) {
        echo do_shortcode('[hooky_shortcode]');
    } 
});

Then go to Appearance > Elements and create a new Hook Element.
Set the Hook to Custom and in the field provided add: db_inside_post_loop
Set the Display rules to Front Page.

In the hook text area, add you advert code.

ok thanks
and what if i'm want to add ads to after 5th post then 10th post and 15th post simounteneously ?

and i want to know which file i can edit for this code without wp login (mean from cpanel) ?

and what if i’m want to add ads to after 5th post then 10th post and 15th post simounteneously ?

You need to duplicate the code and convert it, so it creates a hook for 10th post and another hook for 15th post.

and i want to know which file i can edit for this code without wp login (mean from cpanel) ?

Not sure what you mean. But if you want to add PHP functions to your site, you can use one of the methods introduced in the article:
Adding PHP: https://docs.generatepress.com/article/adding-php/

i can't understand with duplicate and convert the code ?
am trying but its showing error

tell me step by step plz

If you want to handle multiple adverts in the loop, then the best option is to use the Ad Inserter plugin.

https://wordpress.org/plugins/ad-inserter/

Its code is built for this kind of thing, theres no benefit in trying to do it with code especially if you're not that confident with adding PHP to your site.

thanks

You're welcome

Hello David

I also try this code and it works only when I'm loggued as an admin ! Users cannot see it ! Why ?:(

Thank you
JMArc

Hi there,

that code doesn't define a user logged in status.
Are you using any cache plugins ? Perhaps a page cache needs clearing.

Hello David,
It's ok now :)
thank you
JM

You're welcome :)

This archived topic is closed to new replies.