WordPress.org

Make WordPress Core

Opened 5 weeks ago

Closed 5 weeks ago

#47563 closed feature request (duplicate)

Update get_template_part() function

Reported by: yanalexandrov Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords:
Focuses: Cc:

Description

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
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 );
}

Change History (2)

#1 @earnjam
5 weeks ago

  • Component changed from General to Themes
  • Keywords needs-refresh removed
  • Version 5.2.2 deleted

#2 @johnbillion
5 weeks ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Thanks for the report @yanalexandrov and welcome to WordPress Trac. This is a duplicate of #13239.

Note: See TracTickets for help on using tickets.