The new generate_do_template_part action
This action, new to GeneratePress 3 premium update looks like is a game changer! This action will allow us to build post loop templates in Block Elements, and then hook them into the theme.
Currently, there is no UI for the action but you can use some code and, the also new in GP3, Block Element to completely replace the post loop with your own creation. As Tom mentions in this post, he will be introducing Template Tags (like in the current Header Element) into Block Elements in an upcoming GPP update that will allow us to dynamically pull in data from the current post object while using GenerateBlock elements!
Meanwhile, you can still take advantage of the generate_do_template_part
action to create some magic. In this example, we are going to replace the 404 page with a custom block element.
Create your beautiful 404 page by going to Appearance -> Elements and setting up a new block element. Set the hook to be a Custom Hook
and select generate_do_template_part
for Custom Hook Name. Finally, select the 404 template
as Location.
Your new block element will now replace the 404 page post loop template!
You can display the search form found on the default 404 page by using a shortcode block. Insert the following code in your functions.php:
function gm_display_search_form() { return get_search_form(false); } add_shortcode('display_search_form', 'gm_display_search_form');
Then, insert a shortcode block and enter [display_search_form]
. Don’t forget to save the page!
I am really looking forward to the generate_do_template_part
action getting more streamlined in the upcoming GPP update and I will be updating the knowledgebase with more articles.
This is great news!
Thanks
I’ve implemented this.
One issue I see is that the default GP content-404.php template includes a search box.
I still wanted to include that in my custom 404 page.
I tried adding the default search block in the block editor, but it has a different class from the GP search form, so it needs to be restyled meaning adding extra css.
Then I thought I should include the same code as GP, so I can either
1. add an html block with the exact html code generated by GP for the Search form
This works but is not as maintainable in terms of if the GP search form changes in the future
or
2. add the PHP code: get_search_form(); to generate the form
But I can’t see a way of adding the PHP code to a block element, so I had to create a hook element to call the get_search_form(); function and place it at the generate_after_main_content hook.
This works but means you can only place the search box after all the other content on the custom 404 page. OK in my case as that is what I wanted, but not very versatile.
All solutions above are less than ideal.
No criticism of your post, just real-world issues I ran into.
Hi Natalie, thanks for your comment. The easiest solution to this is to use a shortcode block after you’ve created a shortcode for the search form. I have updated the post with the solution I mentioned.
Check it out!
Perfect! Thanks for the code.
No problem 🙂