[Resolved] Hook for injecting content into loop

Home Forums Support [Resolved] Hook for injecting content into loop

Home Forums Support Hook for injecting content into loop

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2267884
    Thomas

    Hi,

    I’m trying to inject a banner into the home loop after X post items but can’t find a hook for that. Is there’re a specific GP hook I can use?

    Cheers,
    Thomas

    #2267926
    David
    Staff
    Customer Support

    Hi there,

    GP uses the generate_do_template_part function to insert the content template.
    And within that function there is the: generate_after_do_template_part hook.

    For reference:

    https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/theme-functions.php#L594

    And heres an example PHP Snippet for using it:

    add_action('generate_after_do_template_part',function(){
    
        // Limit to the front page blog
        if ( is_front_page() && is_home() ) {
    
            global $loop_counter;
            $loop_counter++;
    
            // Do something after every 3rd post
            if ( $loop_counter % 3 == 0  ) {
                // Do your thing
            }
    
        }
     
    });
    #2267970
    Thomas

    That works great, thanks!

    I’m using a custom query to fetch a specific post type for sponsored posts. Is there a way to use the layout/template set in the GP blog archive settings? Currently, I use custom HTML mirroring the layout of the regular posts in the loop.

    Cheers,
    Thomas

    #2267979
    David
    Staff
    Customer Support

    Whats the issue ? Is it just the columns not working ?
    If so see here:

    https://docs.generatepress.com/article/using-columns-in-the-blog/#adding-columns-to-your-custom-post-type

    #2268003
    Thomas

    I’m trying to inject a custom post as ad/sponsored post between the regular home page loop items.

    The ad/sponsored post should have the same layout as the regular post items in the loop.

    I can do that via hardcoding the HTML, that way possible later changes in the customizer for the blog archive layout won’t be reflected though.

    #2268048
    David
    Staff
    Customer Support

    As long as your markup matches the GP content.php template:

    https://github.com/tomusborne/generatepress/blob/master/content.php

    Then it will attract the blog settings.
    The only thing you will require is that filter i posted above so your custom post gets the columns class applied to it.

    #2268069
    Thomas

    Ok, thanks. I’m still struggling to achieve this though and wondering if there’s an easier way for this with GP. Right now, I have lots of custom code and problems like the wrong post ID being used when injecting the custom post type inside the regular home loop.

    I could create a GP element but can’t display that inside the loop, only before or after. Also, it will be shown on paginated pages which it shoudn’t.

    Basically, I’m looking to injected a custom post type post (e.g. via category “Sponsored” or the sticky post option) right in the middle of the home page loop that looks like the regular posts.

    That’s most likely outside the scope of the support forum, any further advice would be appreciated though.

    #2268352
    David
    Staff
    Customer Support

    So is it just the one custom post you need to insert ?

    #2268356
    Thomas

    Yes. Like this on the home page:

    Regular post loop item 1
    Regular post loop item 2
    Regular post loop item 3
    -> Injected custom post item (layout like regular post loop items)
    Regular post loop item 4
    Regular post loop item 5
    Regular post loop item 6

    #2268389
    David
    Staff
    Customer Support

    You could add this PHP Snippet:

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

    Then you can use a Block Element to build your ‘banner’.
    And set the Hook to Custom Hook and add: db_inside_post_loop

    #2268997
    Thomas

    That works great, thanks!

    #2269008
    David
    Staff
    Customer Support

    Glad to hear that!

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.