错误404更改固定链接术语自定义术语分类

时间:2021-04-14 作者:Renan Bessa

我试图更改自定义分类法术语的permalink结构,但它返回了404页。我希望URL是:示例。com/产品/类别/类别名称。

我尝试使用术语链接过滤器,我甚至尝试放置一个与自定义帖子类型不同的slug,但是没有成功。我还更新了永久链接上的wp\\U选项

add_action( \'init\', \'register_sps_products_post_type\' );
function register_sps_products_post_type() {
    register_post_type( \'sps-product\',
        array(
            \'labels\' => array(
                \'name\' => \'Products\',
                \'menu_name\' => \'Product Manager\',
                \'singular_name\' => \'Product\',
                \'all_items\' => \'All Products\'
            ),
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'show_in_nav_menus\' => true,
            \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'comments\', \'post-formats\', \'revisions\' ),
            \'hierarchical\' => false,
            \'has_archive\' => \'products\',
            \'taxonomies\' => array(\'product-category\'),
            \'rewrite\' => array( \'slug\' => \'products\' )
        )
    );
    register_taxonomy( \'product-category\', array( \'sps-product\' ),
        array(
            \'labels\' => array(
                \'name\' => \'Product Categories\',
                \'menu_name\' => \'Product Categories\',
                \'singular_name\' => \'Product Category\',
                \'all_items\' => \'All Categories\'
            ),
            \'public\' => true,
            \'hierarchical\' => true,
            \'show_ui\' => true,
            \'rewrite\' => array( \'slug\' => \'%sps-product%/category\', \'with_front\' => false ),
        )
    );
}

add_filter(\'term_link\', \'idinheiro_permalink_archive_cpt\', 10, 2);
function idinheiro_permalink_archive_cpt( $url ) {
    if ( false !== strpos( $url, \'%sps-product%\') ) {
        $url = str_replace( \'%sps-product%\', \'products\', $url );
    }
    return $url;
}

1 个回复
SO网友:Sally CJ

如果您只希望permalink类别的形式为:example.com/products/category/<category slug>, 那你就不需要上钩了term_link.

相反,您可以设置分类法的rewrite slugproducts/category 您还需要先注册分类法,然后再注册post类型。这样,WordPress会将请求(在上面的永久链接/URL处)视为(单个)术语请求,而不是post类型的请求。

例如:

function register_sps_products_post_type() {
    register_post_type( \'sps-product\', array(
        \'labels\'  => array( /* your code */ ),
        \'public\'  => true,
        // ... your other args.
        \'rewrite\' => array( \'slug\' => \'products\' ),
    ) );
}

function register_sps_product_category_taxonomy() {
    register_taxonomy( \'product-category\', array( \'sps-product\' ), array(
        \'labels\'  => array( /* your code */ ),
        \'public\'  => true,
        // ... your other args.
        \'rewrite\' => array(
            // Use products/category as the rewrite slug.
            \'slug\'       => \'products/category\',
            \'with_front\' => false,
        ),
    ) );
}

// Register the taxonomy first.
add_action( \'init\', \'register_sps_product_category_taxonomy\' );

// Then register the post type.
add_action( \'init\', \'register_sps_products_post_type\' );

// No need to hook on term_link, thus I commented out this part:
//add_filter(\'term_link\', \'idinheiro_permalink_archive_cpt\', 10, 2);
记住刷新重写规则-只需访问永久链接设置(管理)页面。

相关推荐

Problem with permalinks

我已经更改了类别的基本名称,现在它是“博客”,工作正常。但当我通过/blog/%category%/%postname%/更改结构时。显示404。如果我删除结构中的blog,它会再次工作,但我想使用blog word。问题出在哪里?非常感谢。