[Resolved] Set a page template to use sections?

Home Forums Support [Resolved] Set a page template to use sections?

Home Forums Support Set a page template to use sections?

Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #186776
    Katie Jones

    Hi Tom,

    I’m making a custom page template, and I want it to use sections. The WP user won’t be using the sections, I just will in the template itself. But, just using the sections HTML isn’t enough, there’s more going on in the rest of the theme. How can I programmatically set the page to use sections when the template is set?

    Many thanks,
    Katie

    #186777
    Katie Jones

    Responding to turn replies by email on. Thanks!

    #186807
    Tom
    Lead Developer
    Lead Developer

    Hi Katie,

    So you’re hard coding the sections into the template, but the actual Sections interface won’t be used?

    Basically, you want the page to act like Sections are enabled without actually using the Sections options?

    #186850
    Katie Jones

    Yes, exactly. How can I do this?

    Thanks!

    Katie

    #186880
    Katie Jones

    I think I’m on the right track here – I’m working on setting the post meta “_generate_use_sections” on post save. Will report back when complete –

    #186964
    Tom
    Lead Developer
    Lead Developer
    #187076
    Katie Jones

    Hi Tom,

    This is what I’m using (below), it isn’t working yet. I’m adding the same exact meta that is added when I manually set “use sections” to “yes”, and yet “use sections” displays “no” on the front end when I add the meta programmatically. There must be something else to it – any ideas?

    Thanks,
    Katie

    ====

    function zs_set_sections($post_id, $post, $update) {

    $template = get_post_meta($post_id, “_wp_page_template”, true);
    if($template == “tmpl_landing.php”) {
    add_post_meta($post_id, “_generate_use_sections”, ‘a:1:{s:12:”use_sections”;s:4:”true”;}’, true);
    }

    }
    add_action( ‘save_post’, ‘zs_set_sections’, 10000, 3 );

    #187112
    Tom
    Lead Developer
    Lead Developer

    Hmm, try using the “Custom Fields” area when adding your page.

    If you can’t see it, click on “Screen Options” at the top right and check the checkbox next to the Custom Fields option.

    Then under “Name”, add: _generate_use_sections

    Under “Value”, add: a:1:{s:12:"use_sections";s:4:"true";}

    See if that works πŸ™‚

    #187139
    Katie Jones

    Hmm, WordPress is both blocking that name & value formatting. So when I set the custom meta programmatically, it’s there in the database, but I can’t set it on the front end.

    Why might the value be in the database, exactly the same as what is set when you choose “yes” in the drop-down, yet “yes” isn’t selected? Is whether or not this template uses sections entirely determined by this single custom field?

    #187144
    Katie Jones

    Got it! It was that I needed to use an array and let WP serialize it, instead of this string. This works!

    function zs_set_sections($post_id, $post, $update) {
    
      $template = get_post_meta($post_id, "_wp_page_template", true);
      if($template == "tmpl_landing.php") {
        add_post_meta($post_id, "_generate_use_sections", array("use_sections" => "true"), true);
      } 
        
    }
    add_action( 'save_post', 'zs_set_sections', 10000, 3 );
    • This reply was modified 5 years, 3 months ago by Tom.
    #187156
    Tom
    Lead Developer
    Lead Developer

    Bam, very nice.

    Thanks for sharing the code!

    #187160
    Katie Jones

    Sure thing, I hope it helps others! I’ve wanted to force the section view display in other instances so I think I’ll be reusing this on other projects myself.

    Thanks!

    #187573
    Katie Jones

    Hi Tom, there’s a second issue – once I have ‘use sections’ set, it uses the page generate-sections-page-template.php, instead of the normal WordPress structure. Any ideas here?

    My goal is to develop a page with the same HTML structure as a sections page, with the sections hardcoded in the template. Just using a template (and not setting ‘use sections’) wasn’t enough because the header and footer html is different for sections pages, too.

    I suppose I could make new header and footer files, though then I’d need to start removing generatepress functions…

    What’s the best way to hard-code sections, the sections view, in a template with minimal interference to generatepress?

    Thanks!

    Katie

    #187574
    Katie Jones

    …. to expand on the past question – the problem I’m having with it using the page generate-sections-page-template.php is that it’s in the plugin, not the theme – so I’m not sure how to override it, and/or how to override it just for a page template –

    Thanks for any ideas!

    #187588
    Tom
    Lead Developer
    Lead Developer

    Hi Katie,

    This is some code I gave someone else – it might help you: https://gist.github.com/generatepress/84bae0e5ac0d94ce46f7

    Let me know πŸ™‚

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