- This topic has 23 replies, 2 voices, and was last updated 8 years, 4 months ago by Tom.
-
AuthorPosts
-
April 14, 2016 at 8:52 am #186776Katie 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,
KatieApril 14, 2016 at 8:52 am #186777Katie JonesResponding to turn replies by email on. Thanks!
April 14, 2016 at 10:41 am #186807TomLead DeveloperLead DeveloperHi 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?
April 14, 2016 at 12:12 pm #186850Katie JonesYes, exactly. How can I do this?
Thanks!
Katie
April 14, 2016 at 1:40 pm #186880Katie JonesI 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 –
April 14, 2016 at 10:53 pm #186964TomLead DeveloperLead DeveloperThat should do it π
April 15, 2016 at 8:59 am #187076Katie JonesHi 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 );April 15, 2016 at 11:10 am #187112TomLead DeveloperLead DeveloperHmm, 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 π
April 15, 2016 at 12:56 pm #187139Katie JonesHmm, 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?
April 15, 2016 at 1:11 pm #187144Katie JonesGot 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 8 years, 6 months ago by Tom.
April 15, 2016 at 2:22 pm #187156TomLead DeveloperLead DeveloperBam, very nice.
Thanks for sharing the code!
April 15, 2016 at 2:29 pm #187160Katie JonesSure 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!
April 17, 2016 at 8:32 pm #187573Katie JonesHi 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
April 17, 2016 at 8:33 pm #187574Katie 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!
April 17, 2016 at 10:53 pm #187588TomLead DeveloperLead DeveloperHi Katie,
This is some code I gave someone else – it might help you: https://gist.github.com/generatepress/84bae0e5ac0d94ce46f7
Let me know π
-
AuthorPosts
- You must be logged in to reply to this topic.