Site logo

Archived topic

Add Hook In-between Second & Third Post Blog Archives

2 replies · Started by Gareth on November 14, 2022

Viewing posts 1–3 of 3

Hello,

I want to add a hook on my blog archives between the second and third post.

I've been reading the forum and have added the following code below.

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

I'm having problems turning the hook into shortcode if that makes sense. I've manages to get as far is pulling in "name of shortcode" to the desired spot (see image). But cant pull my dynamic block through.

Any advice would be great!

Many Thanks,
Gaz

hook

Hi Gareth,

Try this PHP snippet instead:

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 == 2 ) {
        echo do_shortcode('[hooky_shortcode]');
    } 
});

With this, you'll have a custom action hook called db_inside_post_loop after the 2nd Post in your Archives.

Then, you can use a Block or Hook Element to inject stuff in that specific location.

Just set the Hook name to Custom Hook - db_inside_post_loop.

This archived topic is closed to new replies.