- This topic has 13 replies, 3 voices, and was last updated 4 years, 7 months ago by
Elvin.
-
AuthorPosts
-
October 27, 2021 at 2:38 pm #1979386
Phil
Hi,
Is there a doc that explains how to create a page template for GP 3.1.0?
There is some discussion in the forum spread across questions about different versions of GP, custom posts types vs pages and generate_do_template_part() vs get_template_part(). But it would be really helpful to have some step by step instructions for creating a simple template for customising a page.Thanks,
PhilOctober 27, 2021 at 4:54 pm #1979473David
StaffCustomer SupportHi there,
any particular type of template or better put use case you’re after ?
For example:
are you working with a Custom Post Type that requires its own specific template?
or do you want design your own content template for a specific page or posts within a specific category?
or are you wanting to simply add a predesigned layout to the editor content?sorry for the questioning but there are many flavours of templates and a few ways to do this depending on its use case.
October 27, 2021 at 6:37 pm #1979527Phil
Hi David,
Sorry, I should have been more specific.
I want to add some code (for an Algolia search result) to various parts of the page. There will be breadcrumb navigation, I need to change the <title> tag and after the content area I need to show the details of a record that’s looked up in the Algolia index.
In generic terms, I’m just looking to customise a standard page and add some bits of info to it.
I’ll be using it for one page only, not categories or posts but it would be useful to know how to set up a page template for a category too.Is there a way to select a page template from the page editor or is it only based on the name of the template file(s)?
Thanks,
PhilOctober 27, 2021 at 11:24 pm #1979733Elvin
StaffCustomer SupportHi Phil,
I want to add some code (for an Algolia search result) to various parts of the page.
You can hook in your codes using a Hook Element.
Read more about it here – https://docs.generatepress.com/article/hooks-element-overview/As for hook location reference, you can use this visual guide
https://docs.generatepress.com/article/hooks-visual-guide/Note: It’s not 100% complete but the most used ones are there.
If you have a breadcrumbs shortcode, you can insert that with a Hook element as well.
As for the title tag, it fetches whatever text you’ve added on a post or page’s title field on the backend. If you wish to modify this a bit you’ll need to resort to PHP filter. See my example here – https://generatepress.com/forums/topic/404-page-title-error/#post-1974085
Adding things after the content area can be done w/ either a
Block Element - Hook or aHook Elementas well.For Hook element specifically, you simply add the code that shows your details of a record you’ve looked up on it’s code area and hook it to
generate_after_main_contentIs there a way to select a page template from the page editor or is it only based on the name of the template file(s)?
Hook element have the display rule option through its UI that lets you pick a specific page to apply the hooks, but not the template files used.
If you have a more specific condition for the Hook element, you’ll need to use this filter – https://docs.generatepress.com/article/generate_element_display/
October 28, 2021 at 11:10 am #1981087Phil
Hi Elvin,
I don’t want to use hooks. That will require setting the page ID in the code in multiple places, which does not seem as tidy to me. If I was customising the layout of every page I would do that, but not for one page.
How do I do it with a page template?Thanks,
PhilOctober 28, 2021 at 5:35 pm #1981485Elvin
StaffCustomer SupportI don’t want to use hooks. That will require setting the page ID in the code in multiple places, which does not seem as tidy to me. If I was customising the layout of every page I would do that, but not for one page.
How do I do it with a page template?If the code to be hooked is dynamic then you won’t have to do it for every page. It should be a one and done setup for all taxonomy assuming all the values are fully dynamic and not empty.
If its simple fetching of page or post ID, we can always get it dynamically for each with
get_queried_object_id()orget_the_ID().If you wish to skip using hooks, the only way to go is by creating your own child theme template files for specific post types.
October 28, 2021 at 6:55 pm #1981548Phil
Yes, I created a child theme already. I should have mentioned that too.
My understanding is that I can create a custom template for a specific page url. I don’t want to do it for a custom post type, just a specific page. Is that possible and how do I do that?Thanks,
PhilOctober 28, 2021 at 7:07 pm #1981551Elvin
StaffCustomer SupportYou can copy the default page template
https://github.com/tomusborne/generatepress/blob/master/page.phpand add a condition for the things you want to add.
you can always add this conditional code on certain parts of the page template.
if(is_page( array(123, 456, 789 ) )){ //do your thing here for page ids 123, 456 and 789. } else { // keep the default styling }Say for example, on the template, you want to conditionally add
do_action( 'generate_before_main_content' );You can do something like this –
if(is_page( array(123, 456, 789 ) )){ //do nothing or return; } else { do_action( 'generate_before_main_content' ); }With this concept in mind, we can programmatically add or modifies something based on whatever page id is specified.
And it’s nice because you only need to maintain 1 template plus its dynamic. 😀
October 29, 2021 at 2:48 am #1982039Phil
Hi Elvin,
I don’t want to set the page ID in the code and I don’t want to override the template for all pages.
How do I do it so that I’m only overriding the template for one specific page? (Ideally I want to choose which template is used for a page in the page editor.)
I want to be able to apply GP updates without worrying if I’ve overridden code that does not need to be overridden. I despise overriding core code. That’s worse than using hooks and selecting page IDs in the functions.php file.Based on other posts ( https://generatepress.com/forums/topic/gp-3-0-custom-page-template-how-to/ & https://generatepress.com/forums/topic/custom-page-template/ ) I believe I need to do something like duplicate page.php and content-page.php from the GP theme into my child theme and rename them to page-member-details.php and content-page-member-details.php and then change something in the code so these templates are used by each other and the member-details page. But I don’t understand what to change and it got confusing when talking about different GP versions and generate_do_template_part() and get_template_part().
The standard way to do what I want in WordPress is like this:
https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/
How do I do that in GP?In other WordPress themes you can select a template from the Page Attributes | Template drop down in the editor, but I don’t see that in GP. Is there an equivalent of doing that in GP?
Thanks,
PhilOctober 29, 2021 at 4:55 am #1982253David
StaffCustomer SupportThis topic explains how to create a custom page template:
https://generatepress.com/forums/topic/creating-a-custom-page-template/#post-1875566
October 31, 2021 at 3:53 pm #1985823Phil
Thanks David, that’s perfect. Works nicely.
This would make a good addition to your docs.November 1, 2021 at 4:21 am #1986322David
StaffCustomer SupportGlad to be of help!
November 3, 2021 at 8:10 pm #1990659Phil
Hi Elvin, just wanted to say thanks for all those references to hooks etc. They were helpful too.
November 3, 2021 at 8:11 pm #1990660Elvin
StaffCustomer SupportNo problem. Glad to be of any help. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.