- This topic has 11 replies, 2 voices, and was last updated 1 year, 9 months ago by David.
-
AuthorPosts
-
June 29, 2022 at 3:33 am #2267884Thomas
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,
ThomasJune 29, 2022 at 4:05 am #2267926DavidStaffCustomer SupportHi 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 } } });
June 29, 2022 at 4:58 am #2267970ThomasThat 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,
ThomasJune 29, 2022 at 5:06 am #2267979DavidStaffCustomer SupportWhats the issue ? Is it just the columns not working ?
If so see here:June 29, 2022 at 5:32 am #2268003ThomasI’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.
June 29, 2022 at 6:40 am #2268048DavidStaffCustomer SupportAs 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.June 29, 2022 at 7:01 am #2268069ThomasOk, 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.
June 29, 2022 at 9:14 am #2268352DavidStaffCustomer SupportSo is it just the one custom post you need to insert ?
June 29, 2022 at 9:20 am #2268356ThomasYes. 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 6June 29, 2022 at 9:59 am #2268389DavidStaffCustomer SupportYou 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
June 30, 2022 at 3:27 am #2268997ThomasThat works great, thanks!
June 30, 2022 at 3:47 am #2269008DavidStaffCustomer SupportGlad to hear that!
-
AuthorPosts
- You must be logged in to reply to this topic.