Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created October 17, 2017 13:28
Show Gist options
  • Save vielhuber/b68a8aff9a7f3cf7c0632c37045bc435 to your computer and use it in GitHub Desktop.
Save vielhuber/b68a8aff9a7f3cf7c0632c37045bc435 to your computer and use it in GitHub Desktop.
get permalink / url by template name #wordpress
<?php
function get_page_url($template_name)
{
$pages = get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'meta_query' => [
[
'key' => '_wp_page_template',
'value' => $template_name.'.php',
'compare' => '='
]
]
]);
if(!empty($pages))
{
foreach($pages as $pages__value)
{
return get_permalink($pages__value->ID);
}
}
return get_bloginfo('url');
}
get_page_url('foo');
@VladStorozhenko
Copy link

Thanks for the snippet!

@0xharsha
Copy link

Thank you...

@figitalweb
Copy link

Thanks!!

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