类别固定链接即使在刷新后也不起作用

时间:2020-07-10 作者:gillespieza

我已经创建了一个自定义的帖子类型(公文包),并且正在使用内置的类别分类法。当我查看类别存档页面时,只有常规帖子显示在类别下,我的自定义帖子根本不存在。选择类别的选项在编辑帖子页面(和快速编辑)中,它们只是没有显示在前端。

我已尝试通过在“设置”下保存来刷新永久链接>;Permalinks,我也尝试过包括flush_rewrite_rules(); 在我的功能中。php。什么都没用?

这是我的自定义帖子类型代码(注意:我的自定义帖子可以很好地使用重写规则,只是类别):

/**
 * Registers the \'apc_portfolio\' custom post type.
 *
 * @example register_post_type( $taxonomy, $args );
 * @see https://codex.wordpress.org/register_post_type#Parameters
 * @todo change public, to false
 */
function apc_register_portfolio_post_type() {

    $labels  = array(
        \'add_new_item\'          => __( \'Add New Portfolio Item\', \'detail-factory-2020\' ),
        \'add_new\'               => __( \'Add New Portfolio\', \'detail-factory-2020\' ),
        \'all_items\'             => __( \'All Portfolio Items\', \'detail-factory-2020\' ),
        \'archives\'              => __( \'Portfolio Item Archives\', \'detail-factory-2020\' ),
        \'attributes\'            => __( \'Portfolio Item Attributes\', \'detail-factory-2020\' ),
        \'edit_item\'             => __( \'Edit Portfolio Item\', \'detail-factory-2020\' ),
        \'featured_image\'        => __( \'Featured Image\', \'detail-factory-2020\' ),
        \'filter_items_list\'     => __( \'Filter Portfolio Items list\', \'detail-factory-2020\' ),
        \'insert_into_item\'      => __( \'Insert into Portfolio Item\', \'detail-factory-2020\' ),
        \'items_list_navigation\' => __( \'Portfolio Items list navigation\', \'detail-factory-2020\' ),
        \'items_list\'            => __( \'Portfolio Items list\', \'detail-factory-2020\' ),
        \'menu_name\'             => __( \'Portfolio\', \'detail-factory-2020\' ),
        \'name_admin_bar\'        => __( \'Portfolio Item\', \'detail-factory-2020\' ),
        \'name\'                  => _x( \'Portfolio Items\', \'Post Type General Name\', \'detail-factory-2020\' ),
        \'new_item\'              => __( \'New Portfolio Item\', \'detail-factory-2020\' ),
        \'not_found_in_trash\'    => __( \'Not found in Trash\', \'detail-factory-2020\' ),
        \'not_found\'             => __( \'Not found\', \'detail-factory-2020\' ),
        \'parent_item_colon\'     => __( \'Parent Portfolio Item:\', \'detail-factory-2020\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'detail-factory-2020\' ),
        \'search_items\'          => __( \'Search Portfolio Item\', \'detail-factory-2020\' ),
        \'set_featured_image\'    => __( \'Set featured image\', \'detail-factory-2020\' ),
        \'singular_name\'         => _x( \'Portfolio Item\', \'Post Type Singular Name\', \'detail-factory-2020\' ),
        \'update_item\'           => __( \'Update Portfolio Item\', \'detail-factory-2020\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this Portfolio Item\', \'detail-factory-2020\' ),
        \'use_featured_image\'    => __( \'Use as featured image\', \'detail-factory-2020\' ),
        \'view_item\'             => __( \'View Portfolio Item\', \'detail-factory-2020\' ),
        \'view_items\'            => __( \'View Portfolio Items\', \'detail-factory-2020\' ),
    );
    $rewrite = array(
        \'feeds\'      => false,
        \'pages\'      => false,
        \'slug\'       => \'work\',
        \'with_front\' => true,
    );
    $args    = array(
        \'can_export\'          => true,
        \'capability_type\'     => \'post\',
        \'exclude_from_search\' => false,
        \'has_archive\'         => true,
        \'hierarchical\'        => false,
        \'label\'               => __( \'Portfolio Item\', \'detail-factory-2020\' ),
        \'labels\'              => $labels,
        \'menu_icon\'           => \'dashicons-portfolio\',
        \'menu_position\'       => 5,
        \'public\'              => true,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'show_in_admin_bar\'   => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_rest\'        => true,
        \'show_ui\'             => true,
        \'supports\'            => array(
            \'custom-fields\',
            \'editor\',
            \'excerpt\',
            \'post-formats\',
            \'thumbnail\',
            \'title\',
        ),
        \'taxonomies\'          => array( \'category\' ),
    );
    register_post_type( \'apc-portfolio\', $args );

}
add_action( \'init\', \'apc_register_portfolio_post_type\', 0 );

1 个回复
SO网友:Ben

默认WP类别仅设置为显示开箱即用的帖子。您应该能够通过在functions.php:

// Order course archive and taxonomy views
function adjust_main_query($query){
    if ( ! is_admin() && is_category() && is_category() && $query->is_main_query() ) {

        $query->set( \'post_type\', array( \'post\', \'mpCPT\' ));
    }
}
add_action( \'pre_get_posts\', \'adjust_main_query\' );
我没有对此进行测试,但它是对我使用的一个工作示例的改编。希望它能帮助你走上正确的道路。

相关推荐

Permalinks setting

所以,我知道这是一个基本的问题,但奇怪的是,它对我不起作用。我想要的:https://example.com/blog/thema/类别名称/postname(用于帖子)https://example.com/blog/thema/类别名称(适用于类别)以下是我的设置:遗憾的是,这行不通。以下是有关在什么设置下工作的详细信息:Posts: /blog/thema/%category%/%postname%/ + Category: blog/thema = 分类工作,帖子获取404Posts: /%cat