João Paulo Cercal

Staff Security Engineer at LastPass

  • PHP

How to get a Page Permalink

  • Share on Facebook
  • Share on Twitter
  • Share on LinkedIn

Read this article in

Today we needed to get the permalink of a page in WordPress whose post_type is “page”. To do this, we had to combine two functions, which are: get_permalink() and get_page_by_path().

How to Get a Page Permalink using the Page Name:

1
2
3
<a href="<?php echo get_permalink( get_page_by_path('Eventos') ); ?>">
    <?php _e('Eventos', 'jpcercal.com'); ?>
</a>

How to Get a Page Permalink using the Page Slug:

1
2
3
<a href="<?php echo get_permalink( get_page_by_path('cadastrar-cliente') ); ?>">
    <?php _e('Cadastrar Cliente', 'jpcercal.com'); ?>
</a>

Note About Page Hierarchy and Slugs.

If you have a page hierarchy, you’ll have to pass the complete slug, including the parent page, to the get_page_by_path() function.

For example: to get the permalink of a child page called “Festas” with a parent page called “Eventos”, we would do this:

1
2
3
<a href="<?php echo get_permalink( get_page_by_path( 'eventos/festas' ) ) ?>">
    <?php _e('Eventos', 'jpcercal.com'); ?>
</a>

You can check the functions mentioned above directly in the official WordPress documentation.

That was a very short post, if you have questions, leave a comment!