获取分类法(针对自定义帖子类型)以默认使用父归档样式

时间:2018-02-21 20:21:32

标签: php html wordpress plugins

我刚创建了一个名为research的新自定义帖子类型。我添加了三个分类法,研究领域,作者和出版物。然后我创建了archive-research.php和content-research.php来创建一个不同的风格。我的印象是所有来自研究的分类法都会使用这个档案,所以我要么错了,要么搞砸了。各种分类法的重点是能够创建我可以添加到菜单中的各种列表,但它们最终都使用默认存档。使用自定义存档的唯一方法是访问mywebsite.com/research,但这显示了所有内容。我知道也可以创建自定义分类模板,但是有一些方法可以让分类法默认使用其父存档吗?

无论如何,这是我正在使用的代码,也许我只是遗漏了一些东西,我仍然是wordpress的新手。

function create_research_cpt() {

$labels = array(
    'name' => __( 'Research', 'Post Type General Name', 'textdomain' ),
    'singular_name' => __( 'Research', 'Post Type Singular Name', 'textdomain' ),
    'menu_name' => __( 'Research', 'textdomain' ),
    'name_admin_bar' => __( 'Research', 'textdomain' ),
    'archives' => __( 'Research Archives', 'textdomain' ),
    'attributes' => __( 'Research Attributes', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Research:', 'textdomain' ),
    'all_items' => __( 'All Research', 'textdomain' ),
    'add_new_item' => __( 'Add New Research', 'textdomain' ),
    'add_new' => __( 'Add New', 'textdomain' ),
    'new_item' => __( 'New Research', 'textdomain' ),
    'edit_item' => __( 'Edit Research', 'textdomain' ),
    'update_item' => __( 'Update Research', 'textdomain' ),
    'view_item' => __( 'View Research', 'textdomain' ),
    'view_items' => __( 'View Research', 'textdomain' ),
    'search_items' => __( 'Search Research', 'textdomain' ),
    'not_found' => __( 'Not found', 'textdomain' ),
    'not_found_in_trash' => __( 'Not found in Trash', 'textdomain' ),
    'featured_image' => __( 'Featured Image', 'textdomain' ),
    'set_featured_image' => __( 'Set featured image', 'textdomain' ),
    'remove_featured_image' => __( 'Remove featured image', 'textdomain' ),
    'use_featured_image' => __( 'Use as featured image', 'textdomain' ),
    'insert_into_item' => __( 'Insert into Research', 'textdomain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this Research', 'textdomain' ),
    'items_list' => __( 'Research list', 'textdomain' ),
    'items_list_navigation' => __( 'Research list navigation', 'textdomain' ),
    'filter_items_list' => __( 'Filter Research list', 'textdomain' ),
);
$args = array(
    'label' => __( 'Research', 'textdomain' ),
    'description' => __( 'Studies, Papers, Data & Research', 'textdomain' ),
    'labels' => $labels,
    'menu_icon' => 'dashicons-admin-page',
    'supports' => array('title', 'editor', 'thumbnail', ),
    'taxonomies' => array('field', 'authors', 'publications', ),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'hierarchical' => true,
    'exclude_from_search' => false,
    'show_in_rest' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
);
register_post_type( 'research', $args );

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

function create_field_tax() {

$labels = array(
    'name'              => _x( 'Research Fields', 'taxonomy general name', 'textdomain' ),
    'singular_name'     => _x( 'Research Field', 'taxonomy singular name', 'textdomain' ),
    'search_items'      => __( 'Search Research Fields', 'textdomain' ),
    'all_items'         => __( 'All Research Fields', 'textdomain' ),
    'parent_item'       => __( 'Parent Research Field', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Research Field:', 'textdomain' ),
    'edit_item'         => __( 'Edit Research Field', 'textdomain' ),
    'update_item'       => __( 'Update Research Field', 'textdomain' ),
    'add_new_item'      => __( 'Add New Research Field', 'textdomain' ),
    'new_item_name'     => __( 'New Research Field Name', 'textdomain' ),
    'menu_name'         => __( 'Research Field', 'textdomain' ),
);
$args = array(
    'labels' => $labels,
    'description' => __( '', 'textdomain' ),
    'hierarchical' => true,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_rest' => false,
    'show_tagcloud' => true,
    'show_in_quick_edit' => true,
    'show_admin_column' => false,
);
register_taxonomy( 'field', array('research', ), $args );

}
add_action( 'init', 'create_field_tax' );


function create_author_tax() {

$labels = array(
    'name'              => _x( 'Authors', 'taxonomy general name', 'textdomain' ),
    'singular_name'     => _x( 'Author', 'taxonomy singular name', 'textdomain' ),
    'search_items'      => __( 'Search Authors', 'textdomain' ),
    'all_items'         => __( 'All Authors', 'textdomain' ),
    'parent_item'       => __( 'Parent Author', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Author:', 'textdomain' ),
    'edit_item'         => __( 'Edit Author', 'textdomain' ),
    'update_item'       => __( 'Update Author', 'textdomain' ),
    'add_new_item'      => __( 'Add New Author', 'textdomain' ),
    'new_item_name'     => __( 'New Author Name', 'textdomain' ),
    'menu_name'         => __( 'Author', 'textdomain' ),
);
$args = array(
    'labels' => $labels,
    'description' => __( '', 'textdomain' ),
    'hierarchical' => false,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_rest' => false,
    'show_tagcloud' => true,
    'show_in_quick_edit' => true,
    'show_admin_column' => false,
);
register_taxonomy( 'author', array('research', ), $args );

}
add_action( 'init', 'create_author_tax' );



function create_publication_tax() {

$labels = array(
    'name'              => _x( 'Publications', 'taxonomy general name', 'textdomain' ),
    'singular_name'     => _x( 'Publication', 'taxonomy singular name', 'textdomain' ),
    'search_items'      => __( 'Search Publications', 'textdomain' ),
    'all_items'         => __( 'All Publications', 'textdomain' ),
    'parent_item'       => __( 'Parent Publication', 'textdomain' ),
    'parent_item_colon' => __( 'Parent Publication:', 'textdomain' ),
    'edit_item'         => __( 'Edit Publication', 'textdomain' ),
    'update_item'       => __( 'Update Publication', 'textdomain' ),
    'add_new_item'      => __( 'Add New Publication', 'textdomain' ),
    'new_item_name'     => __( 'New Publication Name', 'textdomain' ),
    'menu_name'         => __( 'Publication', 'textdomain' ),
);
$args = array(
    'labels' => $labels,
    'description' => __( '', 'textdomain' ),
    'hierarchical' => false,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_rest' => false,
    'show_tagcloud' => true,
    'show_in_quick_edit' => true,
    'show_admin_column' => false,
);
register_taxonomy( 'publication', array('research', ), $args );

}
add_action( 'init', 'create_publication_tax' );

1 个答案:

答案 0 :(得分:1)

  

我的印象是所有来自研究的分类法都会   使用此存档

根据doc,您可以使用 archive-research.php 为自定义帖子类型 research 呈现相应的归档索引页。但是,对于自定义分类法,you'd need taxonomy-{taxonomy}.php。所以:

  • 分类法field.php
  • 分类法author.php
  • 分类法publication.php

或者,如果您想要只有一个模板,那么在 archive.php 中,您可以像这样构建循环:(基于存档Twenty Seventeen主题版本1.4的.php 模板

<?php
if ( have_posts() ) : ?>
    <?php
    /* Start the Loop */
    while ( have_posts() ) : the_post();

        if ( 'research' === get_post_type() ) {
            // Load template based on the Post Type.
            get_template_part( 'template-parts/post/content', 'research' );
        } else {
            // Load template based on the Post Format.
            get_template_part( 'template-parts/post/content', get_post_format() );
        }

    endwhile;

else :

    get_template_part( 'template-parts/post/content', 'none' );

endif; ?>

[编辑]参考以下评论:

  

我只是想对研究档案使用不同的外观   反对博客,并不想经历每一个   分类

将其添加到主题的 functions.php 文件中:

add_filter( 'template_include', function ( $template ) {
    if ( is_tax( array( 'field', 'author', 'publication' ) ) ) {
        return locate_template( 'archive-research.php' );
    }
    return $template;
} );