Archived topic
Hook for injecting content into loop
11 replies · Started by Thomas on June 29, 2022
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
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:
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
}
}
});
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
Whats the issue ? Is it just the columns not working ?
If so see here:
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.
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.
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.
So is it just the one custom post you need to insert ?
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
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
That works great, thanks!
Glad to hear that!