How t get Page url with custom template attribute using $wpdb->get_results
-
Im trying to grab the link of a page which uses a custom template using this output
<a href="<?php echo get_template_url('template-contact.php'); ?>">Contact Us</a>
with the function below.(template-contact.php is just an example but I want to use multiple pages in same way)
function get_template_url($template_name){ global $wpdb; $permalink = '#'; // provide a default $RetriveURL = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_value = '$template_name'" ); foreach ($RetriveURL as $slug) { if (get_page($slug->post_id)) { $permalink = get_permalink($slug->post_id); } } return $permalink; }
However, the trick above is working fine but in new installed wordpress sites its not working anymore and its showing default permalink as # instead of the page url(The page is created with the contact template attribute, so page exist in database).
Do you have any idea why this happens and if you have any idea how to make this code working without any problem?
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘How t get Page url with custom template attribute using $wpdb->get_results’ is closed to new replies.