WordPress Get Permalink By Page Name Or Slug

October 19, 2011

development wordpress

I used to write custom functions to return these permalinks from page names or slugs until I discovered you can pass get_permalink() a function that will return the ID.

Here are some examples of getting permalinks by page name or slug in WordPress..

Get WordPress Permalink By Page Name

<a href="<?php echo get_permalink( get_page_by_path( 'Events' ) ); ?>">
    Events
</a>

Get WordPress Permalink By Page Slug

<a href="<?php echo get_permalink( get_page_by_path( 'events' ) ) ?>">
    Events
</a>

Get WordPress Permalink Parent/Child

If you have a page hierarchy you will have to pass the full slug including the parent to the get_page_by_path() function.

Example... To get the permalink using method with child page, you need to pass the full slug. In this case we have a child page called "Parties" with a parent page called "Events".

<a href="<?php echo get_permalink( get_page_by_path( 'events/parties' ) ) ?>">
    Parties
</a>