loading...

Retrieve the permalink for a page and post type.

vbaimas profile image Vasilis Baimas ・1 min read

Do you know that the WordPress have some useful functions to link page and archive page? Let's see them in action.

Scenario :
You would normally grab a page link via its ID but in the case that you had a localhost which install and an online install the ID was different, how you can link the page?

Get the permalink for a page by title

<a href="<?php echo get_permalink( get_page_by_title( 'about' ) ); ?>">About Me</a> 

Get the permalink for a page by name

<a href="<?php echo get_permalink( get_page_by_path( 'About' ) ) ?>">About Me</a>

Get the permalink for a page by slug

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

Get the permalink for a page by parent/child

Scenario :
If you want to get the permalink using the method with child-page, then you need to pass the full slug. So, in this case we have a child page "about" with a parent pages which called "skills".

<a href="<?php echo get_permalink( get_page_by_path( 'about/skills' ) ) ?>">Skills</a

Get the link to custom post type archive page

Scenario :
If you have a custom posts loop and in the bottom you want to add "View all" link, then how you should lead to the page with all posts that type?

 <a href="<?php echo get_post_type_archive_link ( 'your_post_type' ) ?>"</a>

Note:
In case that get_post_type_archive_link() returns false, make sure that you have used the argument has_archive => true when using register_post_type().

Posted on by:

vbaimas profile

Vasilis Baimas

@vbaimas

Pixel Wrangler and WordPress Jedi. Front-End Designer/Developer. Ice Tea drinker, Runner.

Discussion

markdown guide