Last active
August 27, 2019 10:34
-
-
Save morgyface/7fce58edf4d08316fcb45cca75b4a8be to your computer and use it in GitHub Desktop.
WordPress | Get URL from page template name
This file contains hidden or 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 | |
| // 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; | |
| } | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This, ever so slightly extends this gist.