修改后的固定链接结构的自定义帖子类型结果为404

时间:2014-12-29 作者:ph34r

我定义了一个自定义的帖子类型“Review”,我想将其与一个自定义的永久链接:/reviews/year/postname一起使用。下面的代码正确地创建了CPT,并且在创建新的“审阅”时,自定义永久链接正确地显示在管理面板中。但是,当单击自定义永久链接时,创建的结果“Review”返回404。现代wordpress解决这个问题的方式是什么?理想情况下,我不想使用任何外部插件。下面的代码是在自定义插件中定义的。

// Register \'Review\' custom post type
add_action( \'init\', \'vidcreate_post_types\' );

// Register the permalink structure desired for \'Review\' posts, with placeholders
add_action( \'init\', \'vidrewrite_rules\' );

// Replace permalink placeholders with actual content
add_filter(\'post_type_link\', \'update_review_link_placeholders\', 10, 2);



/**
 * Custom \'Review\' post type
 */
function vidcreate_post_types() {

    register_post_type( \'review\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Reviews\' ),
                \'singular_name\' => __( \'Review\' )
            ),
            \'public\' =>  true,
            \'has_archive\' => true,
            \'taxonomies\' => array(\'category\', \'post_tag\'),   
            \'rewrite\' => array(
                \'slug\'=>\'reviews\',
                \'with_front\'=> false,
                \'feed\'=> true,
                \'pages\'=> true
            ),
            \'hierarchical\' => true,
            \'menu_icon\' => \'dashicons-video-alt\',
            \'supports\' => array(
                \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\', \'revisions\', \'page-attributes\'
            )
        )
    );


}


/**
 * Create custom permalink structure for \'Review\' pages
 */
function vidrewrite_rules(){

    add_rewrite_tag( \'%review_slug%\', \'(reviews)\',\'post_type=review&slug=\' );
    add_permastruct( \'review\', \'reviews/%year%/%review%\');

}


/**
 * Update permalinks placeholders with content
 */
function update_review_link_placeholders($permalink, $post) {
    if((\'review\' == $post->post_type) && \'\' != $permalink && !in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\')) ) {

        // currently just replacing %year % with 2014 for testing
        $permalink = str_replace(\'%year%\', \'2014\', $permalink);

        return $permalink;
    }


}

1 个回复
最合适的回答,由SO网友:hunterhogan 整理而成

在register\\u post\\u type中,设置

\'with_front\'=> false,
稍后,您将声明以下不带参数的函数:

add_permastruct( \'review\', \'reviews/%year%/%review%\');
add\\u permastruct的参数为with\\u front,默认值为true,这与之前的声明冲突。

结束

相关推荐

Set a taxonomy as private

我创建了分类增值税,并将public设置为false。但在前端,我可以通过…/?增值税=22。如何保留该分类法供内部使用?下面是我为此分类法创建的代码。add_action( \'init\', \'gp_register_taxonomy_vat\' ); function gp_register_taxonomy_vat() { $labels = array( \'name\' => \'VAT\', \