WordPress.org

Make WordPress Core

Changeset 20075


Ignore:
Timestamp:
03/02/12 18:56:54 (2 years ago)
Author:
nacin
Message:

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.

Location:
trunk/wp-includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-xmlrpc-server.php

    r20074 r20075  
    11781178            $author = get_userdata($page->post_author); 
    11791179 
    1180             $page_template = get_post_meta( $page->ID, '_wp_page_template', true ); 
     1180            $page_template = get_page_template_slug( $page->ID ); 
    11811181            if ( empty( $page_template ) ) 
    11821182                $page_template = 'default'; 
  • trunk/wp-includes/post-template.php

    r19925 r20075  
    488488        if ( is_page_template() ) { 
    489489            $classes[] = 'page-template'; 
    490             $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $page_id, '_wp_page_template', true ) ), '' ); 
     490            $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) ); 
    491491        } else { 
    492492            $classes[] = 'page-template-default'; 
     
    12421242 * @return bool False on failure, true if success. 
    12431243 */ 
    1244 function is_page_template($template = '') { 
    1245     if (!is_page()) { 
     1244function is_page_template( $template = '' ) { 
     1245    if ( ! is_page() ) 
    12461246        return false; 
    1247     } 
    1248  
    1249     global $wp_query; 
    1250  
    1251     $page = $wp_query->get_queried_object(); 
    1252     $custom_fields = get_post_custom_values('_wp_page_template',$page->ID); 
    1253     $page_template = $custom_fields[0]; 
    1254  
    1255     // We have no argument passed so just see if a page_template has been specified 
    1256     if ( empty( $template ) ) { 
    1257         if ( !empty( $page_template ) and ( 'default' != $page_template ) ) { 
    1258             return true; 
    1259         } 
    1260     } elseif ( $template == $page_template) { 
     1247 
     1248    $page_template = get_page_template_slug( get_queried_object_id() ); 
     1249 
     1250    if ( empty( $template ) ) 
     1251        return (bool) $page_template; 
     1252 
     1253    if ( $template == $page_template ) 
    12611254        return true; 
    1262     } 
     1255 
     1256    if ( 'default' == $template && ! $page_template ) 
     1257        return true; 
    12631258 
    12641259    return false; 
     1260} 
     1261 
     1262/** 
     1263 * Get the specific template name for a page. 
     1264 * 
     1265 * @since 3.4.0 
     1266 * 
     1267 * @param int $id The page ID to check. Defaults to the current post, when used in the loop. 
     1268 * @return string|bool Page template filename. Returns an empty string when the default page template 
     1269 *  is in use. Returns false if the post is not a page. 
     1270 */ 
     1271function get_page_template_slug( $post_id = null ) { 
     1272    $post = get_post( $post_id ); 
     1273    if ( 'page' != $post->post_type ) 
     1274        return false; 
     1275    $template = get_post_meta( $post->ID, '_wp_page_template', true ); 
     1276    if ( ! $template || 'default' == $template ) 
     1277        return ''; 
     1278    return $template; 
    12651279} 
    12661280 
  • trunk/wp-includes/post.php

    r20023 r20075  
    53405340    } 
    53415341} 
    5342  
  • trunk/wp-includes/template.php

    r20002 r20075  
    226226function get_page_template() { 
    227227    $id = get_queried_object_id(); 
    228     $template = get_post_meta($id, '_wp_page_template', true); 
     228    $template = get_page_template_slug(); 
    229229    $pagename = get_query_var('pagename'); 
    230230 
    231     if ( !$pagename && $id > 0 ) { 
     231    if ( ! $pagename && $id ) { 
    232232        // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object 
    233233        $post = get_queried_object(); 
     
    235235    } 
    236236 
    237     if ( 'default' == $template ) 
    238         $template = ''; 
    239  
    240     $templates = array(); 
    241     if ( !empty($template) && !validate_file($template) ) 
     237    $templates = array(); 
     238    if ( $template && 0 === validate_file( $template ) ) 
    242239        $templates[] = $template; 
    243240    if ( $pagename ) 
Note: See TracChangeset for help on using the changeset viewer.