Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取wordpress父模板名称_Php_Wordpress - Fatal编程技术网

Php 获取wordpress父模板名称

Php 获取wordpress父模板名称,php,wordpress,Php,Wordpress,我需要获取页面父模板名称。我知道我可以对当前页面使用get_page_template(),但似乎没有办法获取父页面 也可以只获取模板名称而不获取其路径吗?是的,get_page_template()只能返回当前页面的文件路径。改为使用get\u page\u template\u slug(),它只返回文件名,并且可以接受post ID作为参数。您可以将其与wp_get_post_parent_id()结合使用以获取父页面的id You can try this for get parent

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

也可以只获取模板名称而不获取其路径吗?

是的,get_page_template()只能返回当前页面的文件路径。改为使用get\u page\u template\u slug(),它只返回文件名,并且可以接受post ID作为参数。您可以将其与wp_get_post_parent_id()结合使用以获取父页面的id

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;
}
get_page_template_slug(wp_get_post_parent_id(get_the_id()))