You may simply use register_post_type
and register_taxonomy
to do so:
function add_cpt_salads(){
$labels = array(
'name' => __( 'Salads', 'your-text-domain' ),
'singular_name' => __( 'Salad', 'your-text-domain' ),
'menu_name' => __( 'Salads', 'your-text-domain' ),
'all_items' => __( 'All Salads', 'your-text-domain' ),
'add_new' => _x( 'Add new', 'Salads', 'your-text-domain' ),
'add_new_item' => __( 'Add New Salad', 'your-text-domain' ),
'edit_item' => __( 'Edit Salad', 'your-text-domain' ),
'new_item' => __( 'New Salad', 'your-text-domain' ),
'view_item' => __( 'View Salad', 'your-text-domain' ),
'search_items' => __( 'Search Salad', 'your-text-domain' ),
'not_found' => __( 'Not found', 'your-text-domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'your-text-domain' )
);
$args = array(
'label' => __( 'Salads', 'your-text-domain' ),
'labels' => $labels,
'description' => __( 'Salad items', 'your-text-domain' ),
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'register_meta_box_cb' => 'fjn_salads_register_meta_box',
'taxonomies' => array( 'salads_category', 'salads_tag' ),
'has_archive' => true,
'query_var' => true,
'can_export' => true,
);
register_post_type( 'salads', $args );
register_taxonomy(
'salads_category',
'salads',
array(
'label' => __( 'Salads category', 'your-text-domain' ),
'rewrite' => array( 'slug' => 'salad_cat' ),
'hierarchical' => true
)
);
}
add_action( 'init', 'add_cpt_salads', 0 );
UPDATE
You should add the above snippet to your current theme’s functions.php
file.
Then you should go to WordPress admin and under Salads menu, add:
- Best Veg Salads
- Best Non-Veg Salads
- …
- Best XYZ Salads
using Add Salad category submenu.
Then add:
- Salad #1
- Salad #2
- …
- Salad #x
using Add New Salad submenu, and choose categories for them.
Now in the root directory of your current theme, create a new file named page-salads.php
and put this in it:
$terms = get_terms( 'salads_category' );
foreach( $terms as $term ){
$posts = new WP_Query( "taxonomy='salads_category'&term=$term->slug&posts_per_page=-1" );
if( $posts->have_posts() ){?>
<h2><?php echo $term;?></h2><?php
while( $posts->have_posts() ){
$posts->the_post();
$the_title();
//Do you general query loop here ...
}
}
}
Then in WordPress admin, go to Pages and add a page, named Salads, and take care of the slug to be as:
http://{your-home-url}/salads
like:
http://localhost/mywebsite/salads
and save. Then go to Settings>Permalinks and hit the Save changes button and in your browser enter the following URL:
http://localhost/mywebsite/salads
That is all.