最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom post type not using custom template

programmeradmin3浏览0评论

So I've been working on a WordPress site, and I need a custom post for documents, which is supposed to be registered as 'document'. I generated the code for the custom post in a plugin, Meta Box, and placed the code in my functions.php file (though while testing, I disabled Meta Box to see if it was interfering, while keeping the php it generated in functions.php). I have a template, single-document.php file, in the root of my theme (which is a child theme, if that makes a difference). But WordPress keeps defaulting to single.php, even after refreshing permalinks.

This is the code Meta Box generated to add the custom post:

function register_document() {

    $args = array (
        'label' => esc_html__( 'Documents', 'mb86' ),
        'labels' => array(
            'menu_name' => esc_html__( 'Documents', 'mb86' ),
            'name_admin_bar' => esc_html__( 'Document', 'mb86' ),
            'add_new' => esc_html__( 'Add new', 'mb86' ),
            'add_new_item' => esc_html__( 'Add new Document', 'mb86' ),
            'new_item' => esc_html__( 'New Document', 'mb86' ),
            'edit_item' => esc_html__( 'Edit Document', 'mb86' ),
            'view_item' => esc_html__( 'View Document', 'mb86' ),
            'update_item' => esc_html__( 'Update Document', 'mb86' ),
            'all_items' => esc_html__( 'All Documents', 'mb86' ),
            'search_items' => esc_html__( 'Search Documents', 'mb86' ),
            'parent_item_colon' => esc_html__( 'Parent Document', 'mb86' ),
            'not_found' => esc_html__( 'No Documents found', 'mb86' ),
            'not_found_in_trash' => esc_html__( 'No Documents found in Trash', 'mb86' ),
            'name' => esc_html__( 'Documents', 'mb86' ),
            'singular_name' => esc_html__( 'Document', 'mb86' ),
        ),
        'public' => true,
        'description' => 'Handbooks, manuals, and other documents.',
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-book-alt',
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite_no_front' => false,
        'supports' => array(
            'title',
            'thumbnail',
        ),
        'rewrite' => true,
    );

    register_post_type( 'document', $args );
}
add_action( 'init', 'register_document' );

Since most topics addressing this issue focus updating permalinks (which I've repeatedly tried), I'm not really sure how to fix this. Any ideas?

So I've been working on a WordPress site, and I need a custom post for documents, which is supposed to be registered as 'document'. I generated the code for the custom post in a plugin, Meta Box, and placed the code in my functions.php file (though while testing, I disabled Meta Box to see if it was interfering, while keeping the php it generated in functions.php). I have a template, single-document.php file, in the root of my theme (which is a child theme, if that makes a difference). But WordPress keeps defaulting to single.php, even after refreshing permalinks.

This is the code Meta Box generated to add the custom post:

function register_document() {

    $args = array (
        'label' => esc_html__( 'Documents', 'mb86' ),
        'labels' => array(
            'menu_name' => esc_html__( 'Documents', 'mb86' ),
            'name_admin_bar' => esc_html__( 'Document', 'mb86' ),
            'add_new' => esc_html__( 'Add new', 'mb86' ),
            'add_new_item' => esc_html__( 'Add new Document', 'mb86' ),
            'new_item' => esc_html__( 'New Document', 'mb86' ),
            'edit_item' => esc_html__( 'Edit Document', 'mb86' ),
            'view_item' => esc_html__( 'View Document', 'mb86' ),
            'update_item' => esc_html__( 'Update Document', 'mb86' ),
            'all_items' => esc_html__( 'All Documents', 'mb86' ),
            'search_items' => esc_html__( 'Search Documents', 'mb86' ),
            'parent_item_colon' => esc_html__( 'Parent Document', 'mb86' ),
            'not_found' => esc_html__( 'No Documents found', 'mb86' ),
            'not_found_in_trash' => esc_html__( 'No Documents found in Trash', 'mb86' ),
            'name' => esc_html__( 'Documents', 'mb86' ),
            'singular_name' => esc_html__( 'Document', 'mb86' ),
        ),
        'public' => true,
        'description' => 'Handbooks, manuals, and other documents.',
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'show_in_admin_bar' => true,
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-book-alt',
        'capability_type' => 'post',
        'hierarchical' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite_no_front' => false,
        'supports' => array(
            'title',
            'thumbnail',
        ),
        'rewrite' => true,
    );

    register_post_type( 'document', $args );
}
add_action( 'init', 'register_document' );

Since most topics addressing this issue focus updating permalinks (which I've repeatedly tried), I'm not really sure how to fix this. Any ideas?

Share Improve this question asked Jan 26, 2020 at 9:25 MisturDust319MisturDust319 1215 bronze badges 4
  • Could it be your parent theme has hard coded way to call templates and overriding the wp standard templating? – Muhammad Asad Commented Jan 26, 2020 at 10:41
  • 1 The parent theme is Storefront, if that makes a difference – MisturDust319 Commented Jan 26, 2020 at 11:57
  • To list posts, do you have something like a template tpl-document.php ? – Daniel Gross Commented Jan 26, 2020 at 15:49
  • Anyway, look at this developer.wordpress/themes/template-files-section/… – Daniel Gross Commented Jan 26, 2020 at 16:14
Add a comment  | 

1 Answer 1

Reset to default 1

Somewhere along the line, a .php file got changed to a .txt file, and after changing the extension to .php, everything worked fine.

发布评论

评论列表(0)

  1. 暂无评论