I will show You some example of how to add custom post type – ads with related categories. This would be extra post type – like default posts and categories in WordPress.

There are comments for essential parts. Add following code to your functions.php file in your theme.

<?php
/* Ad post type */
// $labels - array with names for your CPT properties
$labels = array(
    'name' => __('My ads', 'your-lang-string'),
    'all_items' => __('Ads', 'your-lang-string'),
    'singular_name' => __('Ad', 'your-lang-string'),
    'add_new' => __('Add New', 'your-lang-string'),
    'add_new_item' => __('Add New Ad', 'your-lang-string'),
    'edit_item' => __('Edit Ad', 'your-lang-string'),
    'new_item' => __('New Ad', 'your-lang-string'),
    'view_item' => __('View Ad', 'your-lang-string'),
    'search_items' => __('Search Ad', 'your-lang-string'),
    'not_found' => __('No Ad found', 'your-lang-string'),
    'not_found_in_trash' => __('No Ad found in Trash', 'your-lang-string')
);

// attributes for your CPT
$args = array(
    'labels' => $labels, // names for your CPT properties
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'show_ui' => true, // whether to generate a default UI for managing this post type in the admin
    'query_var' => true,
    'hierarchical' => true,
    'has_archive' => true, // true if you want archive for your CPT, fox example category view with ads
    'menu_position' => 5, // position of your CPT in WP dashboard menu on the left
    'menu_icon' => '', // you can add icon for your CPT
    'show_in_nav_menus' => true, // true if CPT is available for selection in navigation menus
    'supports' => array(
        'title',
        'editor',
        'thumbnail',
        'author'
    ), // array with things supported by your CPT
    'rewrite' => array(
        'slug' => 'ad'
    ) // slug for your CPT items
);

register_post_type('ad', $args); // finally register your brand new CPT

/* Ads Categories */
// $ads_category_labels - array with names for your CPT taxonomy - kind of category for your CPT items
$ads_category_labels = array(
    'name' => __('Ads Categories', 'your-lang-string'),
    'singular_name' => __('Category', 'your-lang-string'),
    'search_items' => __('Search Categories', 'your-lang-string'),
    'popular_items' => __('Popular Categories', 'your-lang-string'),
    'all_items' => __('All Categories', 'your-lang-string'),
    'parent_item' => __('Parent Category', 'your-lang-string'),
    'parent_item_colon' => __('Parent Category:', 'your-lang-string'),
    'edit_item' => __('Edit Category', 'your-lang-string'),
    'update_item' => __('Update Category', 'your-lang-string'),
    'add_new_item' => __('Add New Category', 'your-lang-string'),
    'new_item_name' => __('New Category Name', 'your-lang-string'),
    'separate_items_with_commas' => __('Separate Categories with commas', 'your-lang-string'),
    'add_or_remove_items' => __('Add or remove Categories', 'your-lang-string'),
    'choose_from_most_used' => __('Choose from the most used Categories', 'your-lang-string'),
    'menu_name' => __('Categories', 'your-lang-string')
);

// register your CPT taxonomy
register_taxonomy('ad_category', array(
    'ad'
), array(
    'hierarchical' => true,
    'labels' => $ads_category_labels, // names for your CPT taxonomy properties
    'show_ui' => true, // whether to generate a default UI for managing this post type in the admin
    'show_in_nav_menus' => true, // true if CPT taxonomy is available for selection in navigation menus
    'query_var' => true,
    'rewrite' => array(
        'slug' => 'ad_category'
    ) // slug for your CPT taxonomy
));
Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *