Skip to content

Instantly share code, notes, and snippets.

@morgyface
Last active August 27, 2019 10:34
Show Gist options
  • Select an option

  • Save morgyface/7fce58edf4d08316fcb45cca75b4a8be to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/7fce58edf4d08316fcb45cca75b4a8be to your computer and use it in GitHub Desktop.
WordPress | Get URL from page template name
<?php
// Get the URL of the first page using a specific template
function url_from_template( $template_name ) {
$args = array(
'post_type' => 'page',
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_wp_page_template',
'meta_value' => $template_name
);
$template_pages = get_posts( $args );
if( $template_pages ) {
$first_id = reset($template_pages);
$permalink = get_permalink($first_id);
return $permalink;
} else {
return null;
}
}
?>
@morgyface
Copy link
Author

This, ever so slightly extends this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment