• Resolved uxarax

    (@uxarax)


    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)
  • Hello @uxarax,

    using this:

    $pages_with_the_contact_template = get_pages( array(
        'meta_key' => '_wp_page_template',
        'meta_value' => 'template-contact.php'
    ) ) ;

    you can get the pages using a certain template. You can loop over it and use get_permalink() to get the URLs.

    If you are confident that only page is using it, you can do something like this:

    $link = get_permalink( $pages_with_the_contact_template[0]->ID )

    I hope this helps.

    Or you can simply use function get_page_link to get the link, like this

    <?php
    echo get_page_link(5);
    ?>

    5 is page id, you can change with yours
    check here

    Thread Starter uxarax

    (@uxarax)

    @implenton Im gonna try your code without the function right now

    @swayam.tejwani thanks for your reply but Im trying to use a function because I want to use multiple templates and the links will be visible on header, so I dont want to add IDs of pages manualy but I want to grab it from the database inestead.

    SO guys the code in my first post is working fine but as I said its not working on all wordpress sites this is why Im confused.

    Thread Starter uxarax

    (@uxarax)

    @implenton your code seems nice and is working fine thankyou

    $contact_template = get_pages( array('meta_key' => '_wp_page_template','meta_value' => 'template-contact.php') ) ;
    $link_contact = get_permalink( $contact_template[0]->ID );
    
    $second_template = get_pages( array('meta_key' => '_wp_page_template','meta_value' => 'template-second.php') ) ;
    $link_second = get_permalink( $second_template[0]->ID );
    
    <?php echo $link_contact; ?>
    
    <?php echo $link_second; ?>
    Thread Starter uxarax

    (@uxarax)

    the only problem that the last way shoing randomly when I use multiple pages

    SO guys the code in my first post is working fine but as I said its not working on all wordpress sites this is why Im confused.

    Do all those websites have the same theme?

    Hello @uxarax,

    you might want to consider passing extra parameters to the get_pages function like sort_column making sure you always get the array of posts back in the order you specified.

    https://codex.www.ads-software.com/Function_Reference/get_pages#Parameters

    Cheers.

    Thread Starter uxarax

    (@uxarax)

    @shariqkhan2012, Yes same theme on all sites

    @implenton your way is working Im very sorry was my fault because didnt create one of these pages and got post permalink instead however its working fine thakyou would be better if you can explain with your experience how to provide a default link if the page is not created.

    thanks again

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.