Archived topic
Custom front page template breaks GP content
4 replies · Started by Sebastian on August 3, 2022
Hi,
I have made a custom page template for the homepage (front-page.php) in order to call another custom function (a AJAX load more feature) by using a template part, amongst other things. In a perfect world this section should load above all other GP created content except for the hero, of which is created with a GP header Element.
My problem is this custom page template (a copy of index.php) seems to break any content created with GP block editor following below the get_template_part().
This is my intended layout:
- Hero section
- Load more posts section
- Our Services section
- Payment Alternatives section
- Become a member section
- Site Footer (GP Elements)
The actual output:
- Hero section
- Load more posts section
- -- not showing up --
- -- not showing up --
- -- not showing up --
- Site Footer (GP Elements)
Best thing would be if I could get the Load More Posts section to load before the actual loop, but I'm not sure how to solve that.
This is the only part differing from the original file:
do_action('generate_before_main_content');
if ( generate_has_default_loop() ) {
if ( have_posts() ) :
/**
* generate_before_loop hook.
*
* @since 3.1.0
*/
do_action( 'generate_before_loop', 'index' );
while ( have_posts() ) :
the_post();
/* This is the new/changed code */
get_template_part('/template-parts/content', 'page');
endwhile;
/**
* generate_after_loop hook.
*
* @since 2.3
*/
do_action( 'generate_after_loop', 'index' );
else :
generate_do_template_part( 'none' );
endif;
}
I'd highly appreciate any help!!
Hi there,
so this:
while ( have_posts() ) :
the_post();
/* This is the new/changed code */
get_template_part('/template-parts/content', 'page');
endwhile;
We would expect to return the GP content template, which is required to output the content you added in the post editor.
You may find it easier to hook your Ajax post loop in before the Post template loop.
Hi David,
Thank you very much for your support!
Ok, so I tried two different ways of doing this.
- Returned the original
generate_do_template_part('index')and simply inserted my own template path just underdo_action('generate_before_loop', 'index'). - By using this filter:
add_filter('the_content', 'add_content_before');
And for clarity, this is what the filter actually looks like:
function add_content_before($content) {
$before_content = get_template_part('/template-parts/content', 'page');
$fullcontent = $before_content . $content ;
return $fullcontent;
}
Both of these gives me the Load more posts as wanted, and the GP block content in plain text(?), you know, like a text string. So I'm clearly missing something here.
Hi,
I found the solution :)
Reading this post I found that I was obviously using the wrong template (index.php) instead of page.php. When using generate_do_template_part( 'page' ); instead, everything works like a charm!
Thank you for awesome support!
Glad to hear you found the solution!!