最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding custom post category slug in permalink causes 404 error for pages and posts

programmeradmin1浏览0评论

I have added a custom post type, I want to add the custom post category in its permalink instead of the static base.

For example for the custom posts I want the following permalink structure:

mysite/category1/custom-post1
mysite/category3/custom-post2
mysite/category4/custom-post3

In the above examples category1, category2 and category3 are the categories of custom post type.

Notice that there is no custom post base slug in the URL.

I am using the following code for it, it works for the custom posts and gets the permalinks correctly, however it gives 404 error for the default posts and pages. How can I fix that so that default posts and pages also work?

Default page/post permalink would be like:

mysite/sample-page
mysite/2021/04/hello-world/

Here's the code that I'm using:

register_post_type( 'business', array( 
    'labels'                 => array(
        'name'               => __( 'Businesses' ),
        'singular_name'      => __( 'Business' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Create New Business' ),
        'edit'               => __( 'Edit' ),
        'edit_item'          => __( 'Edit Business' ),
        'new_item'           => __( 'New Business' ),
        'view'               => __( 'View Businesses' ),
        'view_item'          => __( 'View Business' ),
        'search_items'       => __( 'Search Businesses' ),
        'not_found'          => __( 'No businesses found' ),
        'not_found_in_trash' => __( 'No businesses found in trash' ),
        'parent'             => __( 'Parent Business' ),
    ),
    'description'           => __( 'This is where you can create new businesses on your site.' ),
    'public'                => true,
    'show_ui'               => true,
    'capability_type'       => 'post',
    'publicly_queryable'    => true,
    'exclude_from_search'   => false,
    'has_archive'           => true,
    'menu_position'         => 2,
    'menu_icon'             => 'dashicons-id',
    'hierarchical'          => true,
    '_builtin'              => false,
    'rewrite'               => array( 'slug' => '%business_cat%', 'with_front' => false ),
    'query_var'             => true,
    'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
) );


add_action( 'init', 'create_product_taxonomies', 0 );

function create_product_taxonomies() {
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      =>  __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Categories' ),
        'parent_item_colon' => __( 'Parent Categories:' ),
        'edit_item'         => __( 'Edit Category' ), 
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Category' ),
    );  

    register_taxonomy( 'business_cat', array( 'business' ), array(
        'hierarchical'  => true,
        'labels'        => $labels,
        'show_ui'       => true,
        'query_var'     => true,
        'rewrite'       => array( 'slug' => '%business_cat%', 'with_front' => false ),
    ) );
}

function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'business' ) {
        if ( $terms = get_the_terms( $post->ID, 'business_cat' ) )
            $link = str_replace( '%business_cat%', current( $terms )->slug, $link );
        else
            $link = str_replace( '%business_cat%', 'uncategorized', $link );
    }

    return $link;
}

add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );
发布评论

评论列表(0)

  1. 暂无评论