0

I have created a Custom Post Type Projects. Below is my code. Everything is working fine, but when I click on a Category of this Post Type it redirects me to the Homepage. I have tried all possible methods, but I am still unable to find the issue.

   add_action( 'init', 'create_posttype' );
    function create_posttype() {
     $args = array(
            'labels'             => array('name'=>__('Projects'), 'singular_name'=>__('Projects') ),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' =>'projects','with_front'=>true ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'menu_position'      => null,
            'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
            'taxonomies'          => array( 'project_category','post_tag'),
        );

        register_post_type( 'Projects', $args );

    }

Here is my code for registering the taxonomy,

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


function create_project_taxonomies() {
    $labels = array(
        'name'                       => _x( 'Project category', 'taxonomy general name', 'twentyfifteen' ),
        'singular_name'              => _x( 'Project catgeory', 'taxonomy singular name', 'twentyfifteen' ),
        'search_items'               => __( 'Search category', 'twentyfifteen' ),
        'popular_items'              => __( 'Popular Writers', 'twentyfifteen' ),
        'all_items'                  => __( 'All Category', 'twentyfifteen' ),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __( 'Edit category', 'textdomain' ),
        'update_item'                => __( 'Update catrgory', 'textdomain' ),
        'add_new_item'               => __( 'Add New category', 'textdomain' ),
        'new_item_name'              => __( 'New category Name', 'textdomain' ),
        'separate_items_with_commas' => __( 'Separate writers with commas', 'textdomain' ),
        'add_or_remove_items'        => __( 'Add or remove category', 'textdomain' ),
        'choose_from_most_used'      => __( 'Choose from the most used writers', 'textdomain' ),
        'not_found'                  => __( 'No category found.', 'textdomain' ),
        'menu_name'                  => __( 'Project Category', 'textdomain' ),

    );

    $args = array(
        'hierarchical'          => true,
        'labels'                => $labels,
        'public'                => false,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'rewrite'               => array( 'slug' => 'project_category'),
    );

    register_taxonomy( 'project_category', 'Projects', $args );
}

Please guys, help me.

2 Answers 2

0

Change 'public' => false, to 'public' => true, when using register_taxonomy(). Also, don't use capital letters for the post type name in register_post_type(). Here's the full updated code (make sure to save permalinks after updating your code):

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

add_action( 'init', 'create_posttype' );
    function create_posttype() {
     $args = array(
                    'labels'             => array('name'=>__('Projects'), 'singular_name'=>__('Projects') ),
                    'public'             => true,
                    'publicly_queryable' => true,
                    'show_ui'            => true,
                    'show_in_menu'       => true,
                    'query_var'          => true,
                    'rewrite'            => array( 'slug' =>'projects','with_front'=>true ),
                    'capability_type'    => 'post',
                    'has_archive'        => true,
                    'hierarchical'       => false,
                    'menu_position'      => null,
                    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
                    'taxonomies'          => array( 'project_category','post_tag'),
            );

            register_post_type( 'projects', $args );

    }


function create_project_taxonomies() {
    $labels = array(
            'name'                       => _x( 'Project category', 'taxonomy general name', 'twentyfifteen' ),
            'singular_name'              => _x( 'Project catgeory', 'taxonomy singular name', 'twentyfifteen' ),
            'search_items'               => __( 'Search category', 'twentyfifteen' ),
            'popular_items'              => __( 'Popular Writers', 'twentyfifteen' ),
            'all_items'                  => __( 'All Category', 'twentyfifteen' ),
            'parent_item'                => null,
            'parent_item_colon'          => null,
            'edit_item'                  => __( 'Edit category', 'textdomain' ),
            'update_item'                => __( 'Update catrgory', 'textdomain' ),
            'add_new_item'               => __( 'Add New category', 'textdomain' ),
            'new_item_name'              => __( 'New category Name', 'textdomain' ),
            'separate_items_with_commas' => __( 'Separate writers with commas', 'textdomain' ),
            'add_or_remove_items'        => __( 'Add or remove category', 'textdomain' ),
            'choose_from_most_used'      => __( 'Choose from the most used writers', 'textdomain' ),
            'not_found'                  => __( 'No category found.', 'textdomain' ),
            'menu_name'                  => __( 'Project Category', 'textdomain' ),

    );

    $args = array(
            'hierarchical'          => true,
            'labels'                => $labels,
            'public'                => false,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'update_count_callback' => '_update_post_term_count',
            'query_var'             => true,
            'rewrite'               => array( 'slug' => 'project_category'),
    );

    register_taxonomy( 'project_category', 'projects', $args );
}

Using a singular name for post type and category names is also a good guideline, but not a requirement.

6
  • Thank you for help @Dave. But it didn't work. i have created Category.php, category-project_category.php, taxonomy.php, taxonomy-project_category.php. BUt nothing helped. still facing same problem.
    – mrcoder
    Oct 5, 2016 at 5:50
  • Hrmm, just double-checked, it's working for me -- I created a project named project 1 and assigned it to the Project Cat 1 Project Category. The Project Cat 1 archive URL is working; it is: http://domain.com/project_category/project-cat-1/ Also the taxonomy-project_category.php template is loaded. How are you generating the Project Category URLs? Do they match the same pattern that I showed? Oct 5, 2016 at 6:13
  • [localhost/site/project_category/post1 ] here is link, in my case
    – mrcoder
    Oct 5, 2016 at 6:36
  • Did not understand what is the issue, but found alternative solution, using Numeric Permalink Structure , everyting is working only the problem is [localhost/nieuw**archives**/project_category/rail-ns-4000] archives in link. this worked for me. didn't able to find the issue.
    – mrcoder
    Oct 5, 2016 at 6:47
  • Sorry I forgot to accept the answer, it worked thanks, I did not know what was the problem, I shut down the computer and turned on after few hours and it was working... Surprise for me, it may be the cache.
    – mrcoder
    May 28, 2018 at 6:56
0

Did you try updating the Permalinks under Settings? There is a section at the bottom of that page that points to Category Base. I don't think you have to enter anything into that field, but maybe just click "Save Changes" at the bottom of the page. Hope that does it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.