Introduce get_page_template_slug( $id = null ) to return a page's template (like "showcase.php"). Returns false if post ID is not a page, and an empty string for the default page template. Use the function across core. props billerickson for initial patch. fixes #18750.

git-svn-id: http://svn.automattic.com/wordpress/trunk@20075 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2012-03-02 18:56:54 +00:00
parent 73938cd0a9
commit ae8af5d0dd
4 changed files with 34 additions and 24 deletions

View File

@ -1177,7 +1177,7 @@ class wp_xmlrpc_server extends IXR_Server {
// Get the author info.
$author = get_userdata($page->post_author);
$page_template = get_post_meta( $page->ID, '_wp_page_template', true );
$page_template = get_page_template_slug( $page->ID );
if ( empty( $page_template ) )
$page_template = 'default';

View File

@ -487,7 +487,7 @@ function get_body_class( $class = '' ) {
}
if ( is_page_template() ) {
$classes[] = 'page-template';
$classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $page_id, '_wp_page_template', true ) ), '' );
$classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) );
} else {
$classes[] = 'page-template-default';
}
@ -1242,26 +1242,40 @@ function get_the_password_form() {
* @return bool False on failure, true if success.
*/
function is_page_template( $template = '' ) {
if (!is_page()) {
if ( ! is_page() )
return false;
$page_template = get_page_template_slug( get_queried_object_id() );
if ( empty( $template ) )
return (bool) $page_template;
if ( $template == $page_template )
return true;
if ( 'default' == $template && ! $page_template )
return true;
return false;
}
global $wp_query;
$page = $wp_query->get_queried_object();
$custom_fields = get_post_custom_values('_wp_page_template',$page->ID);
$page_template = $custom_fields[0];
// We have no argument passed so just see if a page_template has been specified
if ( empty( $template ) ) {
if ( !empty( $page_template ) and ( 'default' != $page_template ) ) {
return true;
}
} elseif ( $template == $page_template) {
return true;
}
/**
* Get the specific template name for a page.
*
* @since 3.4.0
*
* @param int $id The page ID to check. Defaults to the current post, when used in the loop.
* @return string|bool Page template filename. Returns an empty string when the default page template
* is in use. Returns false if the post is not a page.
*/
function get_page_template_slug( $post_id = null ) {
$post = get_post( $post_id );
if ( 'page' != $post->post_type )
return false;
$template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( ! $template || 'default' == $template )
return '';
return $template;
}
/**

View File

@ -5339,4 +5339,3 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
}
}

View File

@ -225,20 +225,17 @@ function get_front_page_template() {
*/
function get_page_template() {
$id = get_queried_object_id();
$template = get_post_meta($id, '_wp_page_template', true);
$template = get_page_template_slug();
$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
if ( ! $pagename && $id ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = get_queried_object();
$pagename = $post->post_name;
}
if ( 'default' == $template )
$template = '';
$templates = array();
if ( !empty($template) && !validate_file($template) )
if ( $template && 0 === validate_file( $template ) )
$templates[] = $template;
if ( $pagename )
$templates[] = "page-$pagename.php";