plugin development – Custom post type category link redirecting to 404 page

Question

I have created a custom post type. There is no issue with post and it’s working. I am getting issue in the catgory links. When i click on the Category name it’s redirecting on 404 page

I am getting category link like https://example.co/blog/categoryname/

I have tried to reset the peramlink but still getting issues.

function create_blog_cpt() {

  $labels = array(
    'name' => _x( 'blogs', 'Post Type General Name', 'blogsdomain' ),
    'singular_name' => _x( 'blog', 'Post Type Singular Name', 'blogsdomain' ),
    'menu_name' => _x( 'Blogs', 'Admin Menu text', 'blogsdomain' ),
    'name_admin_bar' => _x( 'blog', 'Add New on Toolbar', 'blogsdomain' ),
    'archives' => __( 'blog Archives', 'blogsdomain' ),
    'attributes' => __( 'blog Attributes', 'blogsdomain' ),
    'parent_item_colon' => __( 'Parent blog:', 'blogsdomain' ),
    'all_items' => __( 'All blogs', 'blogsdomain' ),
    'add_new_item' => __( 'Add New blog', 'blogsdomain' ),
    'add_new' => __( 'Add New', 'blogsdomain' ),
    'new_item' => __( 'New blog', 'blogsdomain' ),
    'edit_item' => __( 'Edit blog', 'blogsdomain' ),
    'update_item' => __( 'Update blog', 'blogsdomain' ),
    'view_item' => __( 'View blog', 'blogsdomain' ),
    'view_items' => __( 'View blogs', 'blogsdomain' ),
    'search_items' => __( 'Search blog', 'blogsdomain' ),
    'not_found' => __( 'Not found', 'blogsdomain' ),
    'not_found_in_trash' => __( 'Not found in Trash', 'blogsdomain' ),
    'featured_image' => __( 'Featured Image', 'blogsdomain' ),
    'set_featured_image' => __( 'Set featured image', 'blogsdomain' ),
    'remove_featured_image' => __( 'Remove featured image', 'blogsdomain' ),
    'use_featured_image' => __( 'Use as featured image', 'blogsdomain' ),
    'insert_into_item' => __( 'Insert into blog', 'blogsdomain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this blog', 'blogsdomain' ),
    'items_list' => __( 'blogs list', 'th-blogs' ),
    'items_list_navigation' => __( 'blogs list navigation', 'blogsdomain' ),
    'filter_items_list' => __( 'Filter blogs list', 'blogsdomain' ),
  );
  $args = array(
    'label' => __( 'blog', 'blogsdomain' ),
    'description' => __( '', 'blogsdomain' ),
    'labels' => $labels,
    'menu_icon' => 'dashicons-book-alt',
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail'),
    'taxonomies' => array('blogs_cats'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'hierarchical' => false,
    'exclude_from_search' => false,
    'show_in_rest' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
  );
  register_post_type( 'blog', $args );

}
add_action( 'init', 'create_blog_cpt', 0 );

register category

function create_blog_taxonomy() {
    register_taxonomy(
        'blogs_cats',
        'blog',
        array(
            'label' => __( 'Category' ),
            'rewrite' => array( 'slug' => 'blog' ),
            'show_ui'           => true,
            'hierarchical' => true,
            'show_in_rest' => true,
            'show_admin_column' => true,

        )
    );
}
add_action( 'init', 'create_blog_taxonomy' );

I am using below code

$args_cats = array('taxonomy' => 'blogs_cats');
$cat = get_terms($args_cats);
      foreach ($cat as $catVal) { 
echo '<a href="'.get_term_link($catVal->term_id,'blogs_cats').'">Category name</a>';
}

0
Naren Verma 3 months 2022-04-30T04:46:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse