Wordpress获取分配给定模板的页面的URL

问题描述 投票:2回答:2

有没有人知道如何获取给定模板分配给的页面的URL。

例如:

模板名称:tpl_gallery.php(问题)网址:gallery.html(答案应该是)

更多解释:

function getTplPageURL($TEMPLATE_NAME){
    $url;

    //Code which i need

    return $url;
}
wordpress
2个回答
1
投票

你听起来像是get_page_link(),这里有更详细的描述:

http://codex.wordpress.org/Function_Reference/get_page_link

如果您在模板的循环中使用它,它将为您提供您所在页面的URL。

<?php get_page_link(); ?>

编辑:好的,我误解了请求。这是另一种基于WP StackExchange答案的方法:https://wordpress.stackexchange.com/a/39657

function getTplPageURL($TEMPLATE_NAME){
    $url;

    //Code which i need

     $pages = query_posts(array(
         'post_type' =>'page',
         'meta_key'  =>'_wp_page_template',
         'meta_value'=> $TEMPLATE_NAME
     ));

     // cycle through $pages here and either grab the URL
     // from the results or do get_page_link($id) with 
     // the id of the page you want 

     $url = null;
     if(isset($pages[0])) {
         $url = get_page_link($pages[0]['id']);
     }
     return $url;
 }

0
投票

我改变了这个功能,因为它对我不起作用:

function getTplPageURL($TEMPLATE_NAME){
    $url = null;
    $pages = get_pages(array(
        'meta_key' => '_wp_page_template',
        'meta_value' => $TEMPLATE_NAME
    ));
    if(isset($pages[0])) {
        $url = get_page_link($pages[0]->ID);
    }
    return $url;
}

现在对wordpress 4.9.4很好

© www.soinside.com 2019 - 2024. All rights reserved.