Make WordPress Core

Ticket #40969: 40969.4.diff

File 40969.4.diff, 1.3 KB (added by tferry, 6 years ago)

Adds a new has_template_part() function, which determines whether a template part file exists

  • src/wp-includes/general-template.php

     
    112112}
    113113
    114114/**
     115 * Determines whether a template part exists.
     116 *
     117 * Checks the named template part for a theme or, if a name is specified, a
     118 * specialised part will be checked. If the theme contains no {slug}.php file
     119 * then this returns false.
     120 *
     121 * For the $name parameter, if the file is called "{slug}-special.php" then specify
     122 * "special".
     123 *
     124 * For more information on similar theme functions, check out
     125 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
     126 * Conditional Tags} article in the Theme Developer Handbook.
     127 *
     128 * @since 5.2.0
     129 *
     130 * @param string $slug The slug name for the generic template.
     131 * @param string $name The name of the specialised template.
     132 */
     133function has_template_part( $slug, $name = null ) {
     134        $templates = array();
     135        $name      = (string) $name;
     136        if ( '' !== $name ) {
     137                $templates[] = "{$slug}-{$name}.php";
     138        }
     139        $templates[] = "{$slug}.php";
     140
     141        return (bool) locate_template( $templates, false, false );
     142}
     143
     144/**
    115145 * Loads a template part into a template.
    116146 *
    117147 * Provides a simple mechanism for child themes to overload reusable sections of code