Make WordPress Core


Ignore:
Timestamp:
03/02/2012 06:56:54 PM (13 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.