【问题标题】:CPT not using archive-{post_type}.phpCPT 不使用存档-{post_type}.php
【发布时间】:2018-10-09 13:14:16
【问题描述】:

我在 WordPress 中设置了自定义帖子类型(下面的代码),并在我的主题中设置了 archive-team.php,但是当我访问 example.com/meet-the-team/ 时,它没有使用应有的存档模板做。

它以前工作但不再工作,它只是使用 page.php 模板。

function cptui_register_my_cpts_team() {

/**
 * Post Type: Team Members.
 */

$labels = array(
    "name" => __( "Team Members", "" ),
    "singular_name" => __( "Team Member", "" ),
);

$args = array(
    "label" => __( "Team Members", "" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => "meet-the-team",
    "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" => "team", "with_front" => true ),
    "query_var" => true,
    "menu_icon" => "dashicons-groups",
    "supports" => array( "title", "page-attributes" ),
);

register_post_type( "team", $args );
}

add_action( 'init', 'cptui_register_my_cpts_team' );

我已经刷新了永久链接并尝试再次添加 CPT,但没有任何乐趣。

【问题讨论】:

  • 您实际使用的是什么文件名? archive-team.php 还是 archive-meet-the-team.php?
  • 存档-team.php

标签: wordpress custom-post-type


【解决方案1】:
"has_archive" => "meet-the-team",

错了。根据wordpress docs,这应该是布尔值或可翻译的。同样正如 M Hayward 所说,如果您要让档案成为团队见面会,那么这需要是档案模板的文件名。

【讨论】:

  • 将 has_archive 设置为字符串/slug 适用于我在同一主题中拥有的其他 CPT,正如我在问题中提到的那样,它之前确实使用了完全相同的设置?
  • 它有效,但它仍然是错误的。根据文档If string, it should be translatable.。尝试将您的文件更改为archive-meet-the-team.php
【解决方案2】:

感谢您的帮助/建议,但我设法解决了问题。

我更改了以下内容:

"rewrite" => array( "slug" => "team", "with_front" => true ),

到这里:

"rewrite" => array( "slug" => "team", "with_front" => false ),

所以完整的 CPT 寄存器函数如下所示:

function cptui_register_my_cpts_team() {

/**
 * Post Type: Team Members.
 */

$labels = array(
    "name" => __( "Team Members", "" ),
    "singular_name" => __( "Team Member", "" ),
);

$args = array(
    "label" => __( "Team Members", "" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "has_archive" => "meet-the-team",
    "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" => "team", "with_front" => false ),
    "query_var" => true,
    "menu_icon" => "dashicons-groups",
    "supports" => array( "title", "page-attributes" ),
);

register_post_type( "team", $args );
}

add_action( 'init', 'cptui_register_my_cpts_team' );

这是使用archive-team.php

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多