获取wordpress父模板名称 [英] Get wordpress parent template name

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

问题描述

我需要获取页面父模板名称.我知道我可以将 get_page_template() 用于当前页面,但似乎没有办法让父母使用.

I need to get the pages parent template name. I know I can use get_page_template() for the current page, but there doesn't seem to be a way to get the parents one.

是否也可以只获取模板名称而不是路径?

Is it also possible to get just the templates name instead of the path to it?

推荐答案

You can try this for get parent page template name
/********** GET PAGES BY PARAMS ************/

/*-- Get root parent of a page --*/
function get_root_page($page_id) 
{
    global $wpdb;

    $parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");

    if ($parent == 0) 
        return $page_id;
    else 
        return get_root_page($parent);
}

/*-- Get page name by ID --*/
function get_page_name_by_ID($page_id)
{
    global $wpdb;
    $page_name = $wpdb->get_var("SELECT post_title FROM $wpdb->posts WHERE ID = '$page_id'");
    return $page_name;
}

/*-- Get page ID by Page Template --*/
function get_page_ID_by_page_template($template_name)
{
    global $wpdb;
    $page_ID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '$template_name' AND meta_key = '_wp_page_template'");
    return $page_ID;
}

/* -- Get page ID by Custom Field Value -- */
function get_page_ID_by_custom_field_value($custom_field, $value)
{
    global $wpdb;
    $page_ID = $wpdb->get_var(" 
        SELECT wposts.ID
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id 
        AND wpostmeta.meta_key = '$custom_field' 
        AND (wpostmeta.meta_value like '$value,%' OR wpostmeta.meta_value like '%,$value,%' OR wpostmeta.meta_value like '%,$value' OR wpostmeta.meta_value = '$value')        
        AND wposts.post_status = 'publish' 
        AND wposts.post_type = 'page'
        LIMIT 0, 1");

    return $page_ID;
}

这篇关于获取wordpress父模板名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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