0

I want to create a post type / format where I can create list type posts... For example...

h1.Title: The List Post Title like "Best Salads..."
- Intro paragraph
h2. Best Veg Salads
- Intro
-- h3. Salad #1
-- h3. Salad #2
..
-- h3. Salad #X

h2. Best Non-Veg Salads
- Intro
-- h3. NV Salad #1
-- h3. NV Salad #2
..
-- h3. MV Salad #X
..
..
h2. Best XYZ Salads
- Intro
-- h3. Salad #1
-- h3. Salad #2
..
-- h3. Salad #X

Footer text...

Essentially each entry is a text field. So I should be able to add any number of headings and subheadings in the post edit screen.

Note: I am not trying to create a post-type as salad. Salad is an example of one of the many published list posts. I am trying to create a site where I can post list type posts...

How can I achieve this.... Any ideas and suggestions would be great...

1
  • Was my answer helpful to you?! Please accept and upvote if was helpful – Pmpr Mar 28 '20 at 16:07
1

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.

4
  • Note: I am not trying to create a post-type as salad. Salad is one of the list posts. I am trying to create a site where I can post list type posts... – Debajyoti Das Feb 1 '16 at 17:28
  • Please note the first sentence in your question: I want to create a post type. So what? – Pmpr Feb 1 '16 at 17:30
  • Sorry, I am new to post type / post formats... Essentially, I am want to create a Top 10 list kind of post... where each entry is editble. hope you understand – Debajyoti Das Feb 1 '16 at 17:37
  • Check the updated answer – Pmpr Feb 1 '16 at 18:08
0

You could use the default blog posts / custom post type and make use of the custom taxonomy and create a hierarchy of your list types.

You can then achieve your page by using a shortcode to pull in your categories ( Salads) and children (Best Non-Veg Salads) .

Within your shortcode you could use get_terms()

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.