The question:
Is it possible to get a page’s permalink from the slug alone? I’m aware that you can get the page’s permalink from the ID using get_page_link()
:
<a href="<?php echo get_page_link(40); ?>" rel="nofollow noreferrer noopener">Map</a>
I’m curious if there is any way to do the same with the slug of a page – like this:
<a href="<?php echo get_page_link('map'); ?>" rel="nofollow noreferrer noopener">Map</a>
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
Is this what you are looking for:
get_permalink( get_page_by_path( 'map' ) )
get_permalink( get_page_by_title( 'Map' ) )
home_url( '/map/' )
Method 2
I think this could be better:
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, $post_type ) );
if ( $page )
return get_page($page, $output);
return null;
}
following the pattern of “original” get_page_by_title
of wordpress. (line 3173)
rgds
Method 3
This is a method published by Tom McFarlin on his blog:
/**
* Returns the permalink for a page based on the incoming slug.
*
* @param string $slug The slug of the page to which we're going to link.
* @return string The permalink of the page
* @since 1.0
*/
function wpse_4999_get_permalink_by_slug( $slug, $post_type = '' ) {
// Initialize the permalink value
$permalink = null;
// Build the arguments for WP_Query
$args = array(
'name' => $slug,
'max_num_posts' => 1
);
// If the optional argument is set, add it to the arguments array
if( '' != $post_type ) {
$args = array_merge( $args, array( 'post_type' => $post_type ) );
}
// Run the query (and reset it)
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$query->the_post();
$permalink = get_permalink( get_the_ID() );
wp_reset_postdata();
}
return $permalink;
}
It works with custom post types and built-in post types (such as post
and page
).
Method 4
Try This:
<a href="<?php echo get_page_link( get_page_by_path( 'map' ) ); ?>" rel="nofollow noreferrer noopener">Map</a>
get_page_by_path( 'path' )
returns page/post object which can be then used by get_page_link()
as it accepts post/page object and returns permalink.
Method 5
the accepted answer is wrong because hierarchical pages don’t work like that. Simply put, the slug is not always the path of the page or post. E.g. your page has a child etc. the path will be parent-slug/child-slug
and get_page_by_path
will fail to find child-slug
this way. The proper solution is this:
function mycoolprefix_post_by_slug($the_slug, $post_type = "page"){
$args = array(
'name' => $the_slug,
'post_type' => $post_type,
'post_status' => 'publish',
'numberposts' => 1
);
$my_page = get_posts($args)[0];
return $my_page;
}
<a href="<?php echo mycoolprefix_post_by_slug('map'); ?>" rel="nofollow noreferrer noopener">Map</a>
Method 6
function theme_get_permalink_by_title( $title ) {
// Initialize the permalink value
$permalink = null;
// Try to get the page by the incoming title
$page = get_page_by_title( strtolower( $title ) );
// If the page exists, then let's get its permalink
if( null != $page ) {
$permalink = get_permalink( $page->ID );
} // end if
return $permalink;
} // end theme_get_permalink_by_title
Use this function by
if( null == theme_get_permalink_by_title( 'Register For This Site' ) ) {
// The permalink doesn't exist, so handle this however you best see fit.
} else {
// The page exists, so do what you need to do.
} // end if/else
Method 7
A little late, but kind of…
You can do this:
<?php $map = get_page_by_title( 'map' ); ?>
<a href="<?php echo get_page_link('$map->ID'); ?>">Map</a>
That’s how I do it 🙂
Thanks,
Josh
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0