[wp-hackers] get_page_link by page name/slug?

Lee Kelleher leekelleher at gmail.com
Sun Nov 26 17:38:16 GMT 2006


Does anyone know if there is a way in the existing WP core code to get the
hyperlink to a page by it's slug/name?

The reason for asking is that when I develop/maintain a WP theme, the
post/page IDs are completely out-of-step with the production site, and using
the get_page_link() function only allows for the ID (integer).  So it would
be nice to use the page slug/name as this would work seamlessly across both
dev and production environments.

I have written some code to do this...

function get_page_link_by_slug($page_slug) {
>     global $wpdb;
>     $page_id = 0;
>     $results = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE
> post_status = 'static' AND post_name = '$page_slug'");
>     if ($results) {
>         foreach ($results as $result) {
>             $page_id = $result->ID;
>         }
>     }
>     return get_page_link($page_id);

}
> usage: echo get_page_link_by_slug('about');
>

I based the syntax on the get_linkobjectsbyname() function - which adds
another hit on the database, but it saves duplicating the code that formats
the hyperlink.

Would this be useful in the core code? or does anyone know of a better way
of achieving this?

Cheers,
- Lee

-- 
Lee Kelleher
http://leekelleher.com/


More information about the wp-hackers mailing list