[Resolved] How to create page template in GP 3.1.0

Home Forums Support [Resolved] How to create page template in GP 3.1.0

Home Forums Support How to create page template in GP 3.1.0

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #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,
    Phil

    #1979473
    David
    Staff
    Customer Support

    Hi 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.

    #1979527
    Phil

    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,
    Phil

    #1979733
    Elvin
    Staff
    Customer Support

    Hi 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 a Hook Element as 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_content

    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)?

    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/

    A wise man once said:
    "Have you cleared your cache?"

    #1981087
    Phil

    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,
    Phil

    #1981485
    Elvin
    Staff
    Customer Support

    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?

    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() or get_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.

    A wise man once said:
    "Have you cleared your cache?"

    #1981548
    Phil

    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,
    Phil

    #1981551
    Elvin
    Staff
    Customer Support

    You can copy the default page template
    https://github.com/tomusborne/generatepress/blob/master/page.php

    and 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. 😀

    A wise man once said:
    "Have you cleared your cache?"

    #1982039
    Phil

    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,
    Phil

    #1982253
    David
    Staff
    Customer Support
    #1985823
    Phil

    Thanks David, that’s perfect. Works nicely.
    This would make a good addition to your docs.

    #1986322
    David
    Staff
    Customer Support
    #1990659
    Phil

    Hi Elvin, just wanted to say thanks for all those references to hooks etc. They were helpful too.

    #1990660
    Elvin
    Staff
    Customer Support

    No problem. Glad to be of any help. 😀

    A wise man once said:
    "Have you cleared your cache?"

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.