如何为自定义帖子类型添加或使用类别模板 [英] How to add or use category template for custom post type

查看:162
本文介绍了如何为自定义帖子类型添加或使用类别模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

我为酒店类别添加了自定义帖子类型资源的类别作为酒店得到默认的职位类别设计,但我不会那样做。我喜欢同一设计想要自定义帖子类型资源



这是我的自定义帖子类型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'=> __('所有资源','gucherry-blog'),
'view_item'=> __('查看资源','gucherry-blog '),
'add_new_item'=> __('添加新资源','gucherry-blog'),
'add_new'=> __('添加资源','gucherry-blog' ),
'edit_item'=> __('Edit Resource','g ucherry-blog),
update_item => __(更新资源, gucherry-博客),
search_items => __(搜索资源, gucherry-blog),
not_found => __(未找到, gucherry-blog),
not_found_in_trash => __(在垃圾箱中找不到, gucherry-blog),
);

$ args = array(
'label'=> __('resources','gucherry-blog'),
'description'=> __('Resource用于软件产品和在线, gucherry-blog),
标签 => $ 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'=>否,
'publicly_queryable'=> true,
'菜单_icon’=> ‘dashicons-admin-site-alt2’,
‘capability_type’=> page,
);

register_post_type('resources',$ args);

}

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

函数resource_categories_taxonomy(){

$ resource_cats = array(
'name'=> _x('Resource Categories','taxonomy general name',' gucherry-blog'),
'singular_name'=> _x('资源类别','分类学单数名称','gucherry-blog'),
'search_items'=> __('搜索资源类别','gucherry-blog'),
'all_items'=> __('All Resource','gucherry-blog'),
'parent_item'=> __('父资源','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'=> __('新资源','gucherry-blog'),
'menu_name'=> __('资源类别','gucherry -blog'),
);

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

}
add_action('init','resource_categories_taxonomy',0);





解决方案

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



taxonomy-resource_categories.php



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



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


I would like to create a unique design category for my custom post type. Here I get it design for main post type but the same design not applying for the category pages it takes to the default category page.

My Custom Post Type name resources for that I create a separate designed template archive-resources.php, so it's working fine but for custom post category page I tried in many ways but still I won't get the result (created separate Taxonomy like category-resources.php) but still not works.

Once I added a category for my custom post type resources as hotels for the hotels category I get default post category design but I won't what that. I like the same design want custom post type resources.

This is my custom post type 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 );

解决方案

You want to edit the design of your taxnonomy, so the way the template structure looks like in wordpress, you should name your file:

taxonomy-resource_categories.php

The taxonomy.php is used if you want to edit the archive page of an taxonomy term, if you add the name of the taxonomy, it will only be used for a specific taxonomy and not all taxonomies. So we had to add the name of your custom taxonomy after.

This is a very useful link for your problem, hope it helps you out: https://developer.wordpress.org/themes/basics/template-hierarchy/

这篇关于如何为自定义帖子类型添加或使用类别模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆