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="https://wordpress.stackexchange.com/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

4 Answers
4

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 );
?>

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *