Make WordPress Core

Opened 5 years ago

Last modified 4 years ago

#47563 reopened feature request

Update get_template_part() function

Reported by: yanalexandrov's profile yanalexandrov Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords: needs-refresh dev-feedback
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 (4)

#1 @earnjam
5 years ago

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

#2 @johnbillion
5 years 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.

#3 @tripflex
4 years ago

@johnbillion while this is a duplicate, it's also been 10+ years since that original one was opened with still nothing happening :(

#4 @tripflex
4 years ago

  • Keywords needs-refresh dev-feedback added
  • Resolution duplicate deleted
  • Status changed from closed to reopened

Reopening this one in hopes of getting some kind of movement as #13239 has clearly died (it's 10+ years old with no movement).

This ticket is also specifically around get_template_part whereas #13239 is surrounding locate_template but it seems nothing is going to happen on that end, so maybe we can at least get the ability to filter on template parts so developers don't have to manage the entire template as per my comment here https://core.trac.wordpress.org/ticket/13239#comment:124

Note: See TracTickets for help on using tickets.