0

I am add new post type called journal using below code it works fine. But I now I need to add a single category for this post type. That is I have two types of journal category normal journal and featured journals. But my blog post has some more categories I need to display only normal journal and featured journals on my journal list how can I do this. I am newbie in Wordpress coding. Thanks.

function post_type_journal()
    {
        $args = array(
            'labels' => array(
                'name' => 'Journals',
                'singular_name' => 'Journal',
                'add_new' => 'Add new journal',
                'edit_item' => 'Edit journal',
                'new_item' => 'New journal',
                'view_item' => 'View journal',
                'search_items' => 'Search journal',
                'not_found' => 'No journals found',
                'not_found_in_trash' => 'No journals found in Trash'
            ),
            'public' => true,
            'supports' => array(
                'title',
                'editor',
                'author',
                'comments'
            ),
            'taxonomies' => array('category') // this is IMPORTANT
        );
        register_post_type('Journal', $args);
    }
    add_action('init','post_type_journal');
1

You can add this line to the code which registers your Custom Post Type:

'taxonomies'   => array( 'category' ),

Or you could add Taxonomies rather than categories:

add_action( 'init', 'cpt_type_taxonomy' );
function cpt_type_taxonomy() {

register_taxonomy( 'portfolio-type', 'portfolio',
    array(
        'labels' => array(
            'name'          => _x( 'Types', 'taxonomy general name', 'theme' ),
            'add_new_item'  => __( 'Add New Portfolio Type', 'theme' ),
            'new_item_name' => __( 'New Portfolio Type', 'theme' ),
        ),
        'exclude_from_search' => true,
        'has_archive'         => true,
        'hierarchical'        => true,
        'rewrite'             => array( 'slug' => 'portfolio-type', 'with_front' => false ),
        'show_ui'             => true,
        'show_tagcloud'       => false,
    )
);

}

Then add this line to the code which registers your Custom Post Type.

'taxonomies'   => array( 'portfolio-type' ),

Here's all the code which registers a Custom Post Type:

add_action( 'init', 'register_custom_post_type' );
function register_custom_post_type() {

register_post_type( 'portfolio',
    array(
        'labels' => array(
            'name'          => __( 'Portfolio', 'theme' ),
            'singular_name' => __( 'Portfolio', 'theme' ),
        ),
        'has_archive'  => true,
        'hierarchical' => true,
        'menu_icon'    => 'dashicons-icon-name',
        'public'       => true,
        'rewrite'      => array( 'slug' => 'portfolio', 'with_front' => false ),
        'supports'     => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
        'taxonomies'   => array( 'portfolio-type' ),

    )
);

}
0

Categories are actually a "taxonomy" for the post post type, so you need to add a new taxonomy (eg 'People') for your Journal post type.

eg:

    function people_init() {
// create a new taxonomy
register_taxonomy(
    'people',
    'journal',
    array(
        'label' => __( 'People' ),
        'rewrite' => array( 'slug' => 'person' ),
        'capabilities' => array(
            'assign_terms' => 'edit_guides',
            'edit_terms' => 'publish_guides'
        )
    )
);
}
add_action( 'init', 'people_init' );

See here for more info on taxonomies.

0

Or you can use

<?php register_taxonomy_for_object_type( 'category', 'Journals' ); ?>

to map the categories to your custom post type.

http://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type

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.