自定义帖子类型存档页面/模板

时间:2018-06-25 07:59:35

标签: php wordpress templates custom-post-type

我已经寻找了2天的问题,现在决定在这里提出我的问题,以便我可以找到适当的解决方案或指导。我在stackoverflow上遇到了类似的问题,但是有些问题的解决方案对我不起作用..有些问题的答案不正确。

请仔细检查我的代码,并告诉我我是否在做任何错误。

我的自定义帖子类型为infographics,并为其添加了taxonomy infograph_category。代码在下面。

// For Infograph Custom Post Type
function mtc_custom_post_type_infographs() {
/**
 * Post Type: Infographs.
 */
$labels = array(
    "name" => __( "Infographs", "" ),
    "singular_name" => __( "Infograph", "" ),
);

$args = array(
    "label" => __( "Infographs", "" ),
    "labels" => $labels,
    "description" => "All the infographic content will be posted with this type of post",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => false,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "infographics", "with_front" => true ),
    "query_var" => true,
    "supports" => array( "title", "editor", "thumbnail", "excerpt" ),
    "taxonomies" => array( "infograph_category" ),
);

register_post_type( "infographics", $args );
}

add_action( 'init', 'mtc_custom_post_type_infographs' );

function mtc_infograph_categories() {
/**
 * Taxonomy: infograph categories.
 */
$labels = array(
    "name" => __( "infograph categories", "" ),
    "singular_name" => __( "infograph_category", "" ),
    "all_items" => __( "All Infograph Categories", "" ),
    "edit_item" => __( "Edit infograph category", "" ),
    "view_item" => __( "View infograph category", "" ),
    "update_item" => __( "Update infograph category", "" ),
    "add_new_item" => __( "Add infograph category", "" ),
    "new_item_name" => __( "New infograph category", "" ),
    "parent_item" => __( "Parent infograph category", "" ),
    "parent_item_colon" => __( "Parent infograph category", "" ),
    "search_items" => __( "Search infograph categories", "" ),
    "popular_items" => __( "Popular infograph categories", "" ),
    "add_or_remove_items" => __( "Add or remove infograph categories", "" ),
    "not_found" => __( "No infograph category found", "" ),
    "no_terms" => __( "No infograph categories", "" ),
);

$args = array(
    "label" => __( "infograph categories", "" ),
    "labels" => $labels,
    "public" => true,
    "hierarchical" => false,
    "label" => "infograph categories",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'infograph_category', 'with_front' => true, ),
    "show_admin_column" => false,
    "show_in_rest" => false,
    "rest_base" => "infograph_category",
    "show_in_quick_edit" => false,
);
register_taxonomy( "infograph_category", array( "infographics" ), $args );
}

add_action( 'init', 'mtc_infograph_categories' );

注意:该代码实际上是由CPT插件生成的

我为单个页面和类别页面创建了模板,例如,单个页面single-infographics.php和类别页面taxonomy-infograph_category.php,并且两个模板都可以正常工作。但是,现在我想创建一个模板,自定义帖子类型infographs中的所有帖子都应显示在其中。 我尝试创建一些模板,例如 archive-infographs.php taxonomy-infographs.php taxonomy-infograph_categories.php archive-infograph_categories.php

但没有任何效果。

我只需要一个指导,我在哪里弄错了这段代码有什么问题。 请不要阻止这篇文章,因为我已经提到我已经经历了很多问题并且在谷歌上搜索了很多..但是在我无法为我的问题找到任何合适的解决方案之后,我就在这里发布它!希望您能理解,我会得到适当的指导/解决方案!

谢谢

1 个答案:

答案 0 :(得分:1)

如果您在@BeforeStep中进行CPT UI插件设置,则可以找到所需的确切信息,例如模板层次结构。

只需将Registered Types/Taxes的值从has_archive更改为False,然后查看CPT UI插件中的模板层次结构。您将找到每种类型的所有必需模板。

相关问题