Created
October 17, 2017 13:28
-
-
Save vielhuber/b68a8aff9a7f3cf7c0632c37045bc435 to your computer and use it in GitHub Desktop.
get permalink / url by template name #wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you...