I wanted to know how can i create Custom and display post type in Wordpress
A custom post type is a regular post with a different post_type. Like we have posts and pages in wordpress as default you can create your own custom.
I will show you an example of how to create and display the custom post type in wordpress. We have a plugin CPT UI using which you can create and display it without writing a code, but I would suggest creating and displaying a custom post type by writing a simple code.
Create Custom post type
Let's say you want to create a custom post type for adding reviews from the wordpress backend.
First, you declare the function in the function.php file, which you can find in the theme root folder.
Here is the function to create a custom post type. In this code, you can also see the taxonomy for the review post_type. Taxonomy is the category for that specific post_type.
Just copy-paste it code into your function.php file and you will see a review menu on the left sidebar in the dashboard. See attached
function reviews_init() { $labels = array( 'name' => 'Reviews', 'singular_name' => 'Review', 'add_new' => 'Add New Review', 'add_new_item' => 'Add New Review', 'edit_item' => 'Edit Review', 'new_item' => 'New Review', 'all_items' => 'All Reviews', 'view_item' => 'View Review', 'search_items' => 'Search Reviews', 'not_found' => 'No Reviews Found', 'not_found_in_trash' => 'No Reviews found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Reviews', ); // register post type $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'show_ui' => true, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array('slug' => 'reviews'), 'query_var' => true, 'menu_icon' => 'dashicons-randomize', 'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'thumbnail', 'author', 'page-attributes' ) ); register_post_type( 'reviews', $args ); // register taxonomy register_taxonomy('review_category', 'reviews', array('hierarchical' => true, 'label' => 'Review Category', 'query_var' => true, 'rewrite' => array( 'slug' => 'review_category' ))); } add_action( 'init', 'reviews_init' );
Display Custom post type
After adding the above code to functions.php you have to now write code to display the custom post type on the frontend.
<?php $args = array( 'post_type' => 'reviews', 'posts_per_page' => 10 ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <h2><?php the_title(); ?></h2> <div class="entry-content"> <?php the_content(); ?> </div> <?php endwhile; wp_reset_postdata(); ?> <?php else: ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?>
Add this code to the template file you want to show your reviews on. You need to write some CSS to match your website layout.
Let me know if you want me to add CSS for you, also if you have any questions feel free to ask. Hope this helps and answers your question.
Okay, that makes sense. Thanks for the answers.
Thanks for useful answers
Thanks you! I have been looking for the answer for a long time and finally I got it.
I'll give you an example of how to make a custom post type in WordPress and display it. Although you may create and show a custom post type without writing any code by utilising our plugin CPT UI, I advise writing some straightforward code instead.