【问题标题】:How to add or use category template for custom post type如何为自定义帖子类型添加或使用类别模板
【发布时间】:2020-05-07 16:35:09
【问题描述】:

我想为我的自定义帖子类型创建一个独特的设计类别。在这里,我将它设计为主要帖子类型,但相同的设计不适用于默认类别页面所需的类别页面。

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

一旦我为我的自定义帖子类型 resources 添加了一个类别作为 hotels 用于 hotels 类别,我获得了默认的帖子类别设计,但我赢了不是什么。我喜欢同样的设计想要自定义帖子类型资源

这是我的自定义帖子类型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】:

    您想编辑分类的设计,因此模板结构在 wordpress 中的样子,您应该为文件命名:

    taxonomy-resource_categories.php

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2011-11-19
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 2016-07-19
      相关资源
      最近更新 更多