自定义分类存档页面需要URL中的?post_type=

时间:2021-09-21 作者:HopefullCoder

我已经创建了一个自定义帖子类型;“事件”;使用自定义分类法“;事件类型;。事件帖子的单页工作正常。事件的存档页工作正常。

然而,当我有一个术语;“成人”;我希望这一页能把我带到一个关于“成年人”一词的所有事件的档案馆:

https://website.org/event-types/adults/
这让我想到了;“未找到任何内容”;(我想是404?)页

但这个链接将我带到我想要的确切页面:

https://website.org/event-types/adults/?post_type=events
我不想?post_type=events 在url中。我甚至不知道它为什么会在那里,因为我认为它应该在没有它的情况下自动工作。

这是我的cpt代码:

add_action( \'init\', \'cdf_events_register_post_type\' );
function cdf_events_register_post_type() {
    $args = [
        \'label\'  => esc_html__( \'CDF Events\', \'text-domain\' ),
        \'labels\' => [
            \'menu_name\'          => esc_html__( \'CDF Events\', \'cdf-serene\' ),
            \'name_admin_bar\'     => esc_html__( \'CDF Event\', \'cdf-serene\' ),
            \'add_new\'            => esc_html__( \'Add CDF Event\', \'cdf-serene\' ),
            \'add_new_item\'       => esc_html__( \'Add new CDF Event\', \'cdf-serene\' ),
            \'new_item\'           => esc_html__( \'New CDF Event\', \'cdf-serene\' ),
            \'edit_item\'          => esc_html__( \'Edit CDF Event\', \'cdf-serene\' ),
            \'view_item\'          => esc_html__( \'View CDF Event\', \'cdf-serene\' ),
            \'update_item\'        => esc_html__( \'View CDF Event\', \'cdf-serene\' ),
            \'all_items\'          => esc_html__( \'All CDF Events\', \'cdf-serene\' ),
            \'search_items\'       => esc_html__( \'Search CDF Events\', \'cdf-serene\' ),
            \'parent_item_colon\'  => esc_html__( \'Parent CDF Event\', \'cdf-serene\' ),
            \'not_found\'          => esc_html__( \'No CDF Events found\', \'cdf-serene\' ),
            \'not_found_in_trash\' => esc_html__( \'No CDF Events found in Trash\', \'cdf-serene\' ),
            \'name\'               => esc_html__( \'CDF Events\', \'cdf-serene\' ),
            \'singular_name\'      => esc_html__( \'CDF Event\', \'cdf-serene\' ),
        ],
        \'public\'              => true,
        \'exclude_from_search\' => true,
        \'publicly_queryable\'  => true,
        \'show_ui\'             => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'show_in_rest\'        => true,
        \'capability_type\'     => \'post\',
        \'hierarchical\'        => false,
        \'has_archive\'         => true,
        \'query_var\'           => true,
        \'can_export\'          => true,
        \'rewrite_no_front\'    => false,
        \'show_in_menu\'        => true,
        \'menu_position\'       => 6,
        \'menu_icon\'           => \'dashicons-calendar-alt\',
        \'supports\' => [
            \'title\',
            \'editor\',
            \'author\',
            \'thumbnail\',
            \'comments\',
            \'revisions\',
            \'page-attributes\',
        ],
        
        \'rewrite\' => true
    ];

    register_post_type( \'events\', $args );
}
以及自定义分类法:

function event_types() {

    $labels = array(
        \'name\'                       => _x( \'Event Types\', \'Taxonomy General Name\', \'cdf_text\' ),
        \'singular_name\'              => _x( \'Event Type\', \'Taxonomy Singular Name\', \'cdf_text\' ),
        \'menu_name\'                  => __( \'Event Type\', \'cdf_text\' ),
        \'all_items\'                  => __( \'All Items\', \'cdf_text\' ),
        \'parent_item\'                => __( \'Parent Event Type\', \'cdf_text\' ),
        \'parent_item_colon\'          => __( \'Parent Event Type:\', \'cdf_text\' ),
        \'new_item_name\'              => __( \'New Event Type\', \'cdf_text\' ),
        \'add_new_item\'               => __( \'Add New Event Type\', \'cdf_text\' ),
        \'edit_item\'                  => __( \'Edit Event Type\', \'cdf_text\' ),
        \'update_item\'                => __( \'Update Event Type\', \'cdf_text\' ),
        \'view_item\'                  => __( \'View Event Type\', \'cdf_text\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'cdf_text\' ),
        \'add_or_remove_items\'        => __( \'Add or remove items\', \'cdf_text\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'cdf_text\' ),
        \'popular_items\'              => __( \'Popular Items\', \'cdf_text\' ),
        \'search_items\'               => __( \'Search Items\', \'cdf_text\' ),
        \'not_found\'                  => __( \'Not Found\', \'cdf_text\' ),
        \'no_terms\'                   => __( \'No items\', \'cdf_text\' ),
        \'items_list\'                 => __( \'Items list\', \'cdf_text\' ),
        \'items_list_navigation\'      => __( \'Items list navigation\', \'cdf_text\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => false,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'has_archive\'                => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'show_in_rest\'               => true
    );
    register_taxonomy( \'event-types\', array( \'events\' ), $args );

}
add_action( \'init\', \'event_types\', 0 );
编辑:我通过每次更改时保存设置来刷新永久链接。

我还将注意到,当我转到后端页面编辑/添加术语时,我将鼠标悬停在术语“上”;成人“;,它为我提供了查看选项,并将我带到非工作链接https://website.org/event-types/adults/

1 个回复
SO网友:HopefullCoder

经过数小时的研究,答案很简单

将此更改为false:

\'exclude_from_search\' => false,

然后它就如预期的那样工作了。

相关推荐