WordPress子页面page自动使用父页面page的模板

默认情况下,我们需要给这个栏目的每个页面设置一个模板,如果页面非常多的话就不方便处理了。

实现方法:

通过函数判断一个页面是否存在父页面,如果存在,则指定父页面的模板为本页面的模板。

 function wizhi_use_parent_template() {
global $post;
$curr_tmp = get_post_meta($post->ID, '_wp_page_template', true); //获取页面模板
if($post->post_parent){
$parent_tmp = get_post_meta($post->post_parent, '_wp_page_template', true); //如果有父页面,获取父页面模板
update_post_meta($post->ID, '_wp_page_template', $parent_tmp, $curr_tmp); //设置子页面的模板为父页面的模板
}
}
add_action('save_post','wizhi_use_parent_template');

这种方法适用于子页面未单独设置一个模板,如果需要为其中某个子页面再次单独设置一个模板,则需要再次进行判断子页面是否已经有独立的模板。

#现在前往

精选留言

页面,page,WordPress,自动
sample
2021-05-22
写留言
签到
投稿
QQ咨询
返回顶部