让 WordPress 子页面自动使用父页面的模板
About using WordPress as a CMS and templates
When we use WordPress as a CMS to publish content, we often set templates for pages to adapt to different content. If it is content belonging to a certain category, the category template is often the same. By default, we need to set a template for each page of this category. This can be very cumbersome if there are many sub-pages under a category and you need to click the mouse many times. Is there a way to set the template for the parent page and automatically apply it to the sub-pages?
Of course, there is a solution. By using a function to determine whether a page has a parent page, we can set the parent page's template as the template for this page.
Set the parent page's template as the sub-page's template
function wizhi_use_parent_template(){
global $post;
$curr_tmp = get_post_meta($post->ID, '_wp_page_template', true);
// Get the page template
if($post->post_parent){
$parent_tmp = get_post_meta($post->post_parent, '_wp_page_template', true);
// If there is a parent page, get its template
update_post_meta($post->ID,'_wp_page_template',$parent_tmp,$curr_tmp);
// Set the sub-page's template as the parent page's template
}
}
add_action('save_post', 'wizhi_use_parent_template');
Possible issues of this method
This method is much simpler, but if I need to set a template for a sub-page separately, there will be a problem. No matter how I set it, as soon as I save the article, the sub-page template will be automatically set as the parent page's template.
Is there a way to solve this problem? Of course, there is. We need to first check whether the sub-page has set a template. If it has, use the current template; if it hasn't, automatically use the parent page's template. The specific code is left for savvy netizens to handle.
-
如何创建WordPress古腾堡编辑器的区块(PHP+原生JS) 2024-01-10 01:01:06
-
搭建一个WordPress网站需要多少成本 2023-11-06 00:09:51
-
Symlink介绍(附:如何使用Symlink进行WordPress开发) 2023-11-05 23:38:32
-
让WordPress实现数据库同步的插件:HyperDB 2023-10-24 23:40:49
-
allegro电商平台值得做吗(附:2023年Allegro注册流程指南) 2023-10-08 21:53:39
-
印度跨境电商平台有哪些(附:印度跨境电商做什么产品好) 2023-10-08 21:34:23
-
跨境电商必看的几大海外二手电商平台 2023-10-08 18:04:42
-
WordPress同城互联网产品解决方案:UBASE 2023-10-03 16:40:39
-
WordPress网站的安全插件:wordfence 2023-09-14 09:25:18
-
WordPress 6.3 支持在手动更新插件和主题失败后回滚旧版本 2023-08-28 16:58:45