admin管理员组

文章数量:1279147

Im using wordpress custom post types for "resources"
The custom post type is called "resource"
Then I created 3 different "post templates" for the post type
webinars, news, gated content

Using different POST TEMPLATES for that.
everything works great there is just one thing I can't accomplish
need to disable/remove " Default Template" from the menu

and set "webinar" as the default one..

Im using wordpress custom post types for "resources"
The custom post type is called "resource"
Then I created 3 different "post templates" for the post type
webinars, news, gated content

Using different POST TEMPLATES for that.
everything works great there is just one thing I can't accomplish
need to disable/remove " Default Template" from the menu

and set "webinar" as the default one..

Share Improve this question edited Oct 4, 2021 at 9:50 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Oct 4, 2021 at 9:05 gil hamergil hamer 6713 gold badges21 silver badges37 bronze badges 6
  • Why not just change the webinar template to the default template? – Jacob Peattie Commented Oct 4, 2021 at 9:15
  • Looking in WordPress github, here, there isn't an option to remove the default option, only change the text. What you can do, NOT RECOMMENDED, is to edit that core file (don't slap me for saying that), just take into account that next WordPress update your edit would be removed. – Buttered_Toast Commented Oct 4, 2021 at 9:16
  • the default template is your single-resource.php file, couldn't you put the webinar template in there? – Tom J Nowell Commented Oct 4, 2021 at 9:48
  • @Tom J Nowell I could but the client wants to display "Webinar" in the admin instead of "Default Template" – gil hamer Commented Oct 4, 2021 at 12:22
  • @Buttered_Toast I do want to change only the text and of course don't want to change core files – gil hamer Commented Oct 4, 2021 at 12:22
 |  Show 1 more comment

1 Answer 1

Reset to default 1

If you want to change only the text you can hook into default_page_template_title

add_filter('default_page_template_title', 'bt_change_default_page_template_title', 10, 2);
function bt_change_default_page_template_title ($text, $meta) {
    if ($meta === 'meta-box') return 'My custom template';

    return $text;
}

Add this code into functions.php and thats it.
Tested on a clean wordpress install, this will only change the text and nothing else, functionality will remain the same.

Update

To get this filter to work with ACF we need to make a few changes.

  1. ACF doesn't pass a second argument when using the filter so we need to assing a default value to our second parameter
  2. Update our condition to take that new, empty, parameter into account
add_filter('default_page_template_title', 'bt_change_default_page_template_title', 10, 2);
function bt_change_default_page_template_title ($text, $meta = '') {
    if ($meta === 'meta-box' || empty($meta)) return 'My custom templatee';

    return $text;
}

本文标签: wordpress posts template remove default template from menu