繁体   English   中英

获取分类法(用于自定义帖子类型),默认情况下使用父级存档样式

[英]Get taxonomies (for custom post type) to use parent archive styling by default

我刚刚创建了一种称为研究的新的自定义帖子类型。 我添加了三个分类法,研究领域,作者和出版物。 然后,我创建了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' );

我的印象是,所有研究分类法都将使用此档案

根据文档 ,是的,您可以使用archive-research.php为自定义帖子类型研究呈现适当的存档索引页面。 但是,对于自定义分类法, 您需要taxonomy- {taxonomy} .php 所以:

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

或者,如果只需要一个template ,则可以在archive.php中构建如下所示的Loop:(基于“ 二十一十七”主题版本1.4的archive.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;
} );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM