Get Permalink by Name

September 2nd, 2008

If you have a post or a page and need the permalink for it there are serveral solutions. Here are one of them. I created a name to permalink converter.

Put the following code to the functions.php in your theme folder.

function wt_get_permalink_by_name($page_name)
{
	global $post;
	global $wpdb;
	$pageid_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
	return get_permalink($pageid_name);
}

Call the function like this in your theme:

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

To get all new functions/template tags, install my plugin WP Extra Template Tags.

  1. 2008-11-04 - Merrick

    Wow!! It’s getting better and better. Keep it up man.,