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 16–29 of 29

Hello
about this code wich work fine, I would like to add, lets' say another adds after 9th post (I keep 5th also).
What piece of code should I add please ?
Thank you
MArc

Hi Jmarc,

Would the ad code be the same after the 5th and 9th posts?

I don't understand Fernando ...
Let's say an add 1 after the 5th post and an add2 after the 9th...

Can you try this code:

add_shortcode('portable_hook', function($atts){
	ob_start();
        $atts = shortcode_atts( array(
            'hook_name' => 'no foo'
        ), $atts, 'portable_hook' );
		do_action($atts['hook_name']);
	return ob_get_clean();
});

add_action('generate_after_do_template_part',function(){
    global $loop_counter;
    $loop_counter++;
    if ( $loop_counter == 5 ) {
        echo do_shortcode('[portable_hook hook_name="after_fifth_post"]');
    } else if ( $loop_counter == 9 ) {
        echo do_shortcode('[portable_hook hook_name="after_ninth_post"]');
    } 
});

With this, you'll have these two hooks:

after_fifth_post

after_ninth_post

Thank you Fr=ernando but It doesn't work :(

I edited and tested the code above. Can you try that instead?

Thank you Fernando
How can I put this in the element please ? I'm last

1. Go to Appearance > Elements and create a new Hook Element.
2. Set the Hook to Custom and in the field provided add: after_fifth_post
3. Add your Advertisement code.
4. Set the Display rule location.
5. Repeat steps 1 to 4 for after_ninth_post

Thank you but only the 5th one is online , the other no :(
I'm retrying...

Both are working and showing when I tested the code.

Can you make sure you created two Hook Elements using the two Hooks I provided?

Yes I did Fernando ... don't know why It doesn't work ...
Still looking at It

I guess It's maybe I Use infinite scroll ...
After testing I'ts not that neither...

It's ok ... i had to add more articles in a page ...
Thank's a lot Fernando !

You're welcome, Jmarc!

This archived topic is closed to new replies.