Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need a <a href="Something Like <?get_contact_page>">Contact Page</a> Code that always links to the Contact page, no matter what is it's ID

There's a page template for contact page.

Is it possible to do this?

I'll gladly provide any further information you need :)

Thanks!!

share|improve this question
    
Lucas, please see: How does accepting an answer works? –  brasofilo Feb 21 '13 at 16:09
    
I'm sorry, I've posted the question and slept!! I never fail to accept an answer and thank everyone who helped :) –  Lucas B Feb 21 '13 at 19:17

3 Answers 3

up vote 1 down vote accepted

If the contact page is identified solely by the associated template, you can query for a page with the template name in meta key _wp_page_template:

$args = array(
    'post_type' => 'page',
    'posts_per_page' => 1,
    'meta_query' => array(
        array(
            'key' => '_wp_page_template',
            'value' => 'contact_template.php'
        )
    )
);
$contact_page = new WP_Query( $args );

if( ! empty( $contact_page->posts ) )
    echo get_permalink( $contact_page->post->ID );
share|improve this answer
    
Damn, you beat me! I pretty much provided the same code, except I created a function and account for instances where a template is assigned to more than one page and support returning an array or echoing single keyed values in the return array. –  DigitalSea Feb 21 '13 at 1:48
    
Thank you for your help!! I'll do it right away! –  Lucas B Feb 21 '13 at 19:25

There is a function called get_page_by_title() that you might find useful:

http://codex.wordpress.org/Function_Reference/get_page_by_title

If the title is always "Contact Page" then you can use

<?php $page = get_page_by_title("Contact Page");?>

<a href="<?php echo get_permalink( $page->ID ); ?>">Link to my Contact Page</a>
share|improve this answer
    
This won't work in the instance of a client website and they decide that the page which was called, "Contact Us" when the site was built would read better if it said, "Get in Contact" which would break this code. Good idea in theory, but you should never assume things will be constant (especially page titles). –  DigitalSea Feb 21 '13 at 1:44
    
I agree, I somehow missed the "template" sentence of his question ;-) –  birgire Feb 21 '13 at 1:48
    
Happens to the best of us man! :) –  DigitalSea Feb 21 '13 at 1:50
    
your get_page_by_template() function looks very useful, nice. –  birgire Feb 21 '13 at 1:57
    
Thanks man. It's probably a little rough around the edges, but seems to get the job done! I might go and clean it up a little later because it's a pretty useful function. –  DigitalSea Feb 21 '13 at 2:35

You're in luck. Wordpress stores a pages template value in a hidden custom field not visible in the backend but very much exists called, "_wp_page_template". I created the following function which should return an array or a single key of data for you.

I put the code on Github Gist here because it's a little long. I did some testing and it seems to work! Should do what you want it too.

share|improve this answer
    
Thanks for sharing your knowledge, question answered! –  Lucas B Feb 21 '13 at 19:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.