自定义发布类型404错误,即使在刷新重写规则之后也是如此

时间:2019-05-31 作者:CatK

我已经声明了一个自定义帖子类型,如下所示,我可以在admin中创建帖子,但当我尝试查看时,我得到了404错误。我已经使用类似的代码声明了一个以前的自定义帖子类型,它工作得很好。我不想要归档页,但想要单贴页。

我曾尝试刷新重写规则,并停用插件,但两者都不起作用。任何帮助都将不胜感激!!

//create courses post type
add_action( \'init\', \'create_courses_post_type\' );
function create_courses_post_type() {
    $labels = array(
        \'name\'               => __( \'Courses\' ),
        \'singular_name\'      => __( \'Course\' ),
        \'all_items\'          => __( \'All Courses\' ),
        \'add_new\'            => _x( \'Add New Course\', \'Courses\' ),
        \'add_new_item\'       => __( \'Add New Course\' ),
        \'edit_item\'          => __( \'Edit Course\' ),
        \'new_item\'           => __( \'New Course\' ),
        \'view_item\'          => __( \'View Course\' ),
        \'search_items\'       => __( \'Search in Courses\' ),
        \'not_found\'          => __( \'No Courses found\' ),
        \'not_found_in_trash\' => __( \'No Courses found in trash\' ),
        \'parent_item_colon\'  => \'\'
    );
    $args = array(
        \'labels\'             => $labels,
        \'public\'             => true,
        \'has_archive\'        => false, // Set to false hides Archive Pages
        \'menu_icon\'          => \'dashicons-welcome-learn-more\', //pick one here ~> https://developer.wordpress.org/resource/dashicons/
        \'rewrite\'            => array( \'slug\' => \'courses\' ),
        \'taxonomies\'         => array( \'category\', \'post_tag\' ),
        \'query_var\'          => true,
        \'menu_position\'      => 9,
        \'publicly_queryable\' => true
    );
    register_post_type( \'course\', $args );
}

1 个回复
SO网友:Jayesh

您的代码工作正常。创建“自定义帖子类型”挂钩后,必须刷新/刷新永久链接。

步骤1:在主菜单中找到“Settings>Permalinks”
步骤2:如果需要,向下滚动并单击“保存更改”
第3步:刷新重写规则和永久链接。

相关推荐