[wp-trac] [WordPress Trac] #47563: Update get_template_part() function

WordPress Trac noreply at wordpress.org
Wed Jun 19 12:31:29 UTC 2019


#47563: Update get_template_part() function
-----------------------------+-----------------------------
 Reporter:  yanalexandrov    |      Owner:  (none)
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  General          |    Version:  5.2.2
 Severity:  normal           |   Keywords:  needs-refresh
  Focuses:                   |
-----------------------------+-----------------------------
 I think it would be good to add the ability to programmatically override
 the output of posts in the loop without interfering with the theme files (
 for example, archive.php and others ). This will allow you to change the
 display of posts through plugins.

 **For example.** To adapt the theme to work with Woocommerce, you need to
 edit the theme code. For users, this is sometimes very difficult.
 My code:

 {{{#!php
 <?php
 function get_template_part( $slug, $name = null ) {
         /**
          * Fires before the specified template part file is loaded.
          *
          * The dynamic portion of the hook name, `$slug`, refers to the
 slug name
          * for the generic template part.
          *
          * @since 3.0.0
          *
          * @param string      $slug The slug name for the generic
 template.
          * @param string|null $name The name of the specialized template.
          */
         do_action( "get_template_part_{$slug}", $slug, $name );
         $templates = array();
         $name      = (string) $name;
         if ( '' !== $name ) {
                 $templates[] = "{$slug}-{$name}.php";
         }
         $templates[] = "{$slug}.php";
         /**
          * Fires before a template part is loaded.
          *
          * @since 5.2.0
          *
          * @param string   $slug      The slug name for the generic
 template.
          * @param string   $name      The name of the specialized
 template.
          * @param string[] $templates Array of template files to search
 for, in order.
          */
         do_action( 'get_template_part', $slug, $name, $templates );

         $templates = add_filter( 'get_template_part', $templates );

         locate_template( $templates, true, false );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47563>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list