WordPress.org

Make WordPress Core

Ticket #40969: 40969.5.diff

File 40969.5.diff, 1.4 KB (added by tferry, 5 months ago)

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

  • 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|null $name Optional. The name of the specialised template. Default null.
     132 *
     133 * @return bool Whether the template part exists.
     134 */
     135function has_template_part( $slug, $name = null ) {
     136        $templates = array();
     137        $name      = (string) $name;
     138        if ( '' !== $name ) {
     139                $templates[] = "{$slug}-{$name}.php";
     140        }
     141        $templates[] = "{$slug}.php";
     142
     143        return (bool) locate_template( $templates, false, false );
     144}
     145
     146/**
    115147 * Loads a template part into a template.
    116148 *
    117149 * Provides a simple mechanism for child themes to overload reusable sections of code