如何为自定义帖子类型添加或使用类别模板

问题描述 投票:0回答:1

我想为我的自定义帖子类型创建一个独特的设计类别。在这里,我得到了主要帖子类型的设计,但是没有将其应用于默认类别页面的类别页面使用相同的设计。

我的自定义帖子类型名称resources为此,我创建了一个单独的设计模板archive-resources.php,所以它工作正常,但对于自定义帖子类别页面,我尝试了很多方法,但仍然可以无法获得结果(创建了单独的分类法,例如category-resources.php),但仍然无法正常工作。

一旦为我的自定义帖子类型resources添加一个类别,作为hotels类别的hotels,我将获得默认的帖子类别设计,但我不会那样做。我喜欢相同的设计,希望自定义帖子类型resources

这是我的自定义帖子类型functions.php

function custom_resource_type() {

    $labels = array(
        'name'                => _x( 'Resources', 'Post Type General Name', 'gucherry-blog' ),
        'singular_name'       => _x( 'Resource', 'Post Type Singular Name', 'gucherry-blog' ),
        'menu_name'           => __( 'Resources', 'gucherry-blog' ),
        'parent_item_colon'   => __( 'Parent Resource', 'gucherry-blog' ),
        'all_items'           => __( 'All Resources', 'gucherry-blog' ),
        'view_item'           => __( 'View Resource', 'gucherry-blog' ),
        'add_new_item'        => __( 'Add New Resource', 'gucherry-blog' ),
        'add_new'             => __( 'Add Resource', 'gucherry-blog' ),
        'edit_item'           => __( 'Edit Resource', 'gucherry-blog' ),
        'update_item'         => __( 'Update Resource', 'gucherry-blog' ),
        'search_items'        => __( 'Search Resource', 'gucherry-blog' ),
        'not_found'           => __( 'Not Found', 'gucherry-blog' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'gucherry-blog' ),
    );

    $args = array(
        'label'               => __( 'resources', 'gucherry-blog' ),
        'description'         => __( 'Resource for software products and online', 'gucherry-blog' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( '' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'menu_icon'           => 'dashicons-admin-site-alt2',
        'capability_type'     => 'page',
    );

    register_post_type( 'resources', $args );

}

add_action( 'init', 'custom_resource_type', 0 );

function resource_categories_taxonomy() {

  $resource_cats = array(
    'name' => _x( 'Resource Categories', 'taxonomy general name', 'gucherry-blog' ),
    'singular_name' => _x( 'Resource Category', 'taxonomy singular name', 'gucherry-blog' ),
    'search_items' =>  __( 'Search Resource Categories', 'gucherry-blog' ),
    'all_items' => __( 'All Resource', 'gucherry-blog' ),
    'parent_item' => __( 'Parent Resource', 'gucherry-blog' ),
    'parent_item_colon' => __( 'Parent Resource:', 'gucherry-blog' ),
    'edit_item' => __( 'Edit Resource', 'gucherry-blog' ), 
    'update_item' => __( 'Update Resource', 'gucherry-blog' ),
    'add_new_item' => __( 'Add New Resource', 'gucherry-blog' ),
    'new_item_name' => __( 'New Resource', 'gucherry-blog' ),
    'menu_name' => __( 'Resource Categories', 'gucherry-blog' ),
  );    

  register_taxonomy('resource_categories',array('resources'), array(
    'hierarchical' => true,
    'labels' => $resource_cats,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'resource' ),
  ));

}
add_action( 'init', 'resource_categories_taxonomy', 0 );
wordpress custom-post-type
1个回答
0
投票

您要编辑分类法的设计,因此,模板结构在wordpress中的外观应以文件命名:

taxonomy-resource_categories.php

如果要编辑分类术语的存档页面,则使用taxonomy.php,如果添加分类名称,则仅用于特定分类,而不是所有分类。因此,我们必须在之后添加您的自定义分类法的名称。

这是解决您问题的非常有用的链接,希望它对您有所帮助:https://developer.wordpress.org/themes/basics/template-hierarchy/

© www.soinside.com 2019 - 2024. All rights reserved.