Mar.25

Getting Site permalink url of Associated template page by template_page_name

WordPress have the query function which filters associated template pages. If we require the page URL automatic by using template name we can use like this function to get the associate page link.

/*
* Get the template related page url by template name
* input template name like $TEMPLATE_NAME = template-service.php
* @ returns {siteurl}/service
* Service is page slug which is created in page
*/

function getTplPageURL($TEMPLATE_NAME){

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

$url = null;
if(isset($pages[0])) {
$url = get_page_link($pages[0]->ID);
}
return $url;
}
echo getTplPageURL(template-service.php);
/*
* Returns the associate page_url
* which is created in pages with using this template
*/

Share this Story:
  • facebook
  • twitter
  • gplus

Leave a comment

Facebook Comment