Skip to content

Instantly share code, notes, and snippets.

@Narayon
Created July 5, 2017 09:50
Show Gist options
  • Save Narayon/4b8b1d92ff24e06df20365e1d3a8a9e3 to your computer and use it in GitHub Desktop.
Save Narayon/4b8b1d92ff24e06df20365e1d3a8a9e3 to your computer and use it in GitHub Desktop.
WP - Get link of page with specific template
<?php
function get_template_page_url( $template_name ) {
$url = null;
$pages = new \WP_Query( 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
if ( isset( $pages[0] ) ) {
$url = get_page_link( $pages[0]['id'] );
}
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment