允许的儿童模板 - wordpress [英] Allowed template for children - wordpress

查看:27
本文介绍了允许的儿童模板 - wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法添加/更改页面子项的允许模板?当我为特定页面创建子级时,我想设置要使用的模板.我在使用 Processwire CMS 时有这个选项:

Is there any way to add/change allowed template of page children ? When I create child for specific page I would like to set template to use. I have that option when I use Processwire CMS:

选择允许用于使用此模板的页面的子页面的模板.仅当您特别想限制使用此模板的页面放置时才使用此选项.如果没有选择,则在用户的访问限制内允许任何一个.一个示例用法可能是一个新闻列表"模板,它只允许有使用新闻项目"或新闻发布"模板的子项.

Select the template(s) that will be allowed for children of pages using this template. Use this only if you specifically want to restrict placement of pages using this template. If none are selected then any are allowed, within the user's access limits. An example usage could be a 'news-list' template that is only allowed to have children using 'news-item' or 'press-release' templates.

推荐答案

我以前遇到过这个问题,我使用了一个自定义函数来确定页面处于什么级别,然后基于该级别包含一个文件,使用:

I have come across this issue before and I have used a custom function to determine what level a page is at and then include a file based on that using:

 get_post_ancestors();

函数(添加到functions.php)

The function (added to functions.php)

function hierarchy_level($post){
    if(count(get_post_ancestors($post)) === 0){
        return 'parent';
    }elseif(count(get_post_ancestors($post)) === 1){
        return 'child';
    }
    elseif(count(get_post_ancestors($post)) === 2){
        return 'grandchild';
    }else{
        return false;
    }
}

以及在页面模板中的用法:

And usage in a page template:

$page_level = hierarchy_level($post);

switch ($page_level){
    case 'parent':
        include('template-parts/parent.php');
        break;
    case 'child':
        include('template-parts/child.php');
        break;
    case 'grandchild':
        include('template-parts/grandchild.php');
        break;
}

这篇关于允许的儿童模板 - wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆