Internal Links to Pages in PHP?

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

Leave a Comment