plugins – Adding category base to url in custom post giving 404?

Question

I’m building a mobile site and need to use a custom post
i used to build it with Custom Post Type UI and got the code inside function.php
first time I tried it it was ok and working but the category base was not showing in the URL so I modified the code but after the modifications the URL giving 404 error

function cptui_register_my_cpts() {

    /**
     * Post Type: Phones.
     */

    $labels = [
        "name" => __( "Phones", "jnews" ),
        "singular_name" => __( "Phone", "jnews" ),
        "menu_name" => __( "Phones", "jnews" ),
        "all_items" => __( "All Phones", "jnews" ),
    ];

    $args = [
        "label" => __( "Phones", "jnews" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => true,
        "rewrite" => [ "slug" => "brand/%brand%/phone/", "with_front" => false],
        'has_archive' => 'brand',
        "query_var" => true,
        "supports" => [ "title", "editor", "thumbnail", "excerpt", "custom-fields", "comments", "post-formats" ],
        "taxonomies" => [ "brand" ],
        "show_in_graphql" => false,

    ];

    register_post_type( "phone", $args );
}


add_action( 'init', 'cptui_register_my_cpts' );


function cptui_register_my_taxes_brand() {

    /**
     * Taxonomy: Brands.
     */

    $labels = [
        "name" => __( "Brands", "jnews" ),
        "singular_name" => __( "Brand", "jnews" ),
    ];


    $args = [
        "label" => __( "Brands", "jnews" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => false,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'brand', 'with_front' => true,  'hierarchical' => true, ],
        "show_admin_column" => false,
        "show_in_rest" => true,
        "rest_base" => "brand",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
        "show_in_graphql" => false,
    ];
    register_taxonomy( "brand", [ "phone" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_brand' );

function tm_brands_post_link( $post_link, $id = 0 ){
    $post = get_post($id);
    $terms = wp_get_object_terms( $post->ID, 'brand' );
    if( $terms ){
        return str_replace( '%brand%' , $terms[0]->slug , $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'tm_brands_post_link', 1, 3 );

0
Marco 2 weeks 2021-09-11T18:23:27-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse