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.

What is the best way of linking to WordPress pages with PHP? Considering that I move the page from a local server to a live server to another URL?

<a href="/wordpress/services" title="Read More" class="yellowButton">Read more</a> 

How could you replace this code with PHP linking to the WordPress page.

/wordpress/services
share|improve this question
    
I know it might seem like a silly question, But I would like to know what is the best solution for linking to pages, Do you link directly to a ID or a page name. I don't understand the WordPress Codex page. Could someone please give a PHP example of this. –  Josh May 3 '12 at 9:18
1  
What do you know about the page? Title? Page ID? Nothing? –  Chip Bennett May 3 '12 at 13:27
add comment

1 Answer 1

up vote 2 down vote accepted

Page Permalink from $id

If you know the Page $id, use get_permalink():

<?php $permalink = get_permalink( $id ); ?>

Page Permalink from $slug

If you know the Page $slug, such as /about (including hierarchy, such as /about/work), use get_page_by_path() to determine the Page $id, then use get_permalink().

<?php
$page_object = get_page_by_path( $slug );
$page_id = $page_object->ID;
$permalink = get_permalink( $page_id );
?>

Page Permalink from $title

If you know the Page $title, such as "Some Random Page Name", use get_page_by_title(), then use get_permalink():

<?php
$page_object = get_page_by_title( $title );
$page_id = $page_object->ID;
$permalink = get_permalink( $page_id );
?>
share|improve this answer
    
Thanks for the answer, Very Appreciated. –  Josh May 4 '12 at 8:12
add comment

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.