[Resolved] Old School WordPress Templates

Home Forums Support [Resolved] Old School WordPress Templates

Home Forums Support Old School WordPress Templates

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #2167869
    Tim

    Hi,

    I’m updating an old WordPress site that uses Underscores and templates. For example, it’s an online magazine website and one template displays each issue using a date in the URL. A URL of /february-2022, for example, is parsed to display content that has been published in February 2022. Content is sorted based on date published. And the query is a garden variety WordPress function.

    I don’t see how to use templates in GeneratePress. Specifically, in the Gutenberg interface, the Pages tab in the right side column no longer has a Templates dropdown list. I’m wondering if I’m able to still use templates in GeneratePress or if I have to somehow learn then use the convoluted (to me, at least, by comparison) blocks and whatever approach?

    My question is: can I use templates in Gutenberg and GeneratePress the way they work in WordPress? If so, how do I get them to be recognized and assigned?

    Thanks!

    #2168121
    David
    Staff
    Customer Support

    Hi there,

    especially with the latest version of WP – the word template has a lot of meanings.
    I assume this a a Page Template:

    https://developer.wordpress.org/themes/template-files-section/page-template-files/

    If so then yes GP and modern WP can still use them. Was the original theme a Custom Theme ? How were the Templates added ?

    #2168128
    Tim

    Yes, it’s a page template. I used a bunch of them in a child theme and then assign them in the edit page area for specific pages. They’re all hand-coded template PHP files that use the pro forma coding style of calling the header and footer functions with the template file containing only code for the body of the page.

    I’d like to continue to use them but see two issues. One is how to assign them to a page: the latest version of WordPress I’m using and latest GP no longer has a Template dropdown list on the right side column when I have a page open to edit. And two, if I could use the page template, I don’t see how to use blocks to define how the page output is displayed.

    #2168140
    David
    Staff
    Customer Support

    So in the block editor go to the 3 dot menu ( in right of top bar ) and select preferences. Make sure the Template and Page Attributes panels are enabled.

    #2168147
    Tim

    I clicked the vertical three dots and selected Preferences which displays a popup. Clicking the Panels tab on the left shows that the Templates and Page Attributes are selected.

    When I refresh the page, then click the Page tab in the right side column, I still don’t see Templates listed.

    #2168148
    David
    Staff
    Customer Support

    I assume you added the Templates to the GP Child Theme ? GP doesn’t add any by default.

    #2168151
    Tim

    I copied over several page templates into the child theme I’m working with, refreshed the edit page, clicked the Page tab on the right side and still don’t see a Template heading or dropdown list. Not sure if it would be a heading or part of Page Attributes like parent page but it’s neither on my edit page.

    The templates are using the standard flag up top in the code right below the <?php line:

    /*
    Template Name: Name Here
    */

    #2168183
    David
    Staff
    Customer Support

    Hmmm… see this topic:

    https://generatepress.com/forums/topic/creating-a-custom-page-template/#post-1875566

    its a solution i provided for creating Page Templates.
    I just tested it on a clean install of WP/GP and it still works:

    2022-03-26_16-21-44

    maybe that topic can provide some hints ?

    #2168190
    Tim

    So the Templates dropdown only appears if there are viable page templates in the theme folder? And templates are viable only if they follow some pattern? I wonder what makes a template viable, if that’s the case.

    #2168576
    David
    Staff
    Customer Support

    So the Templates dropdown only appears if there are viable page templates in the theme folder?

    Correct.

    What makes them viable? There is not stipulations from GP on this part.
    I simply followed WPs developers guidelines, but i did use GPs single template and content-single template part for my base.

    #2169056
    Tim

    Thanks David! I’ll go away and play with this, especially your last bit about recreating with GP single and content-single templates, and bug you if there’s anything additional.

    #2169476
    David
    Staff
    Customer Support

    Let us know how you get on!

    #2170610
    Tim

    Hi, I have a presumably quick follow up question about GeneratePress and part of the template. I have the Template dropdown list appearing. Now I want to swap out the default page generation code.

    In the template page, I see a generate_has_default_loop function that apparently is a filter created by GeneratePress (not Automattic), according to this page:

    https://docs.generatepress.com/article/generate_has_default_loop/

    My goal is to have my template display all posts for a specific month and year, using existing code to parse the URL, for example, /february-2022 would generate a month value of 02 and a year value of 2022. That would then be used to pull from WordPress all content published in February 2022.

    Given the generate_has_default_loop page, and the idea of filters, is it correct that I should write a filter with my query to pull stories and pass in the month and year values? Then add the filter? Is that right direction to go?

    Part of my problem, at this point, is that while I’ve used WordPress from the beginning and have long experience using and coding content management systems, I’ve deliberately avoided the approach WordPress has used and currently uses. So I’m learning something that has always felt too abstracted. It’s the reason my templates didn’t work: they’re missing the hook and other calls.

    In any event, let me know if this is the correct direction to go given how WordPress code is structured and GeneratePress is integrated into it. Thx.

    #2170697
    Elvin
    Staff
    Customer Support

    Hi Tim,

    Given the generate_has_default_loop page, and the idea of filters, is it correct that I should write a filter with my query to pull stories and pass in the month and year values? Then add the filter? Is that right direction to go?

    If you’re doing this for static pages, generate_has_default_loop may not be applicable since it’s for archives.

    To add to the conversation:

    Since the goal is to display a list of stories on a static page using a template so it acts like an archive page, you may have to add in a WP_Query() or a get_posts() loop to display them on the page.

    You then assign the query parameter for the loop based on your static page’s permalink. (like /february-2022)

    Within the template file, we can parse the URL’s permalink using this PHP code:

    global $post;
    $parsed_permalink = $post->post_name;

    $parsed_permalink would have the value of february-2022.

    We then make some sort of date recognition using this $parsed_permalink value so it can be used for WP_Query parameters.

    Example code:

        $parsed_month = strtok($parsed_permalink,'-');
        $year = substr($parsed_permalink, strpos($parsed_permalink, '-') + 1);  
        $month = date('n', strtotime($parsed_month));

    We then build the $args for the WP_Query loop using the $year and $month we got from the $parsed_permalink.

    Example code:

        $args = array(
            'date_query' => array(
                array(
                    'year'  => $year,
                    'month' => $month,
                ),
            ),
        );
        $query = new WP_Query( $args );

    Reference – https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

    This way, the WP_Query instance you’re going to use on the template will be a dynamic loop based on the parsed URL of the current static page. 🙂

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

    #2171100
    Tim

    Thanks Elvin! That’s actually the code I’m using with a little more granularity to exclude a few categories. My question is how to integrate what I have with the WordPress template structure and how the templates are coded with hooks etc.

    All of our content is only active on the day we launch a new issue of our bi-monthly online magazine. The next day and every day after the data is essentially archived data. And every online issue of the magazine is made up of 12-15 posts all with the same date, the first day of an even-numbered month. With this new template/design, I’ll use time to sort the 12-15 stories, not date.

    Given that, to grab these posts, what method do you recommend I use that’s WordPress standard? Should I do what I’m doing now, tossing default code in a template and adding code calling a function with code like yours above? Or create a filter to hold the code above and replace the contents of an existing filter?

    Appreciate your help!

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