[wp-trac] [WordPress Trac] #16264: Add function get_pages_by_template()
WordPress Trac
wp-trac at lists.automattic.com
Sun Jan 16 19:38:15 UTC 2011
#16264: Add function get_pages_by_template()
-----------------------------+-----------------------------
Reporter: mikeschinkel | Owner:
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Post Types | Version: 3.1
Severity: normal | Keywords:
-----------------------------+-----------------------------
This patch adds a function `get_pages_by_template()` to `/wp-
includes/post.php`. It accepts a page template string and returns an array
of post objects keyed by the pages that use the template:
{{{
$pages = get_pages_by_template( 'page-about.php' );
}}}
Using WordPress for CMS I've been finding a need to provide links to
specific pages in a more robust way than hardcoding their `post_id` or
even referencing their `post_name`/`URL slug` and the way I've identified
that works well is to locate a page by it's page template. Here a function
that a theme might write that would use the function I'm proposing be
added:
{{{
function get_about_page_link() {
$pages = get_pages_by_template( 'page-about.php' );
if ( count( $pages ) )
return get_page_link( get_post( $pages[0] )->ID);
return false;
}
}}}
Unfortunately WordPress does not have efficient functionality in core
that would allow a themer to lookup a page or a list of pages by their
templates thus requiring the theme developer to resort to SQL which is
ideally avoided.
Thus I'm providing this patch to enable a themer to do this lookup without
having to resort to SQL.
The patch also fixes a typo that affects phpdoc in the same file: @parem
=> @param.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/16264>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list