in ,

Create Custom Post Type In WordPress

Custom post types are new post types you can create. A custom post type can be added to WordPress via the register_post_type() function. This function allows you to define a new post type by its labels, supported features, availability and other specifics.

You can make your blog have a separate post like Movie, Music, Portfolio, Products, etc, by create custom post type. You can customize your custom post type with different custom field and even its own custom category structure. Below you can learn how to create your own custom post type in step by step. If you don’t understand any part please comment on the box below.

Let’s Create a Custom Post Type

Copy the code below to your theme’s function.php

[php]
//create a custom post type Portfolio
function sopheap_portfolio() {
$labels = array(
"name" => "Portfolios", //Plural Label
"singular_name" => "Portfolio", //Singular Label
);

$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "portfolio", "with_front" => true ),
"query_var" => true,
"menu_icon" => "dashicons-chart-bar", //get the icon class from this site (https://developer.wordpress.org/resource/dashicons)
"supports" => array( "title", "editor", "excerpt" ), //you can add more supports like comment, author etc
);
register_post_type( "portfolio", $args );

// End of sopheap_portfolio()
}
add_action( ‘init’, ‘sopheap_portfolio’ );
[/php]

After you done that refresh your WordPress admin you will see the Portfolios on the left side menu.

Display your custom post type on your front page site

If you would like them to display among your regular post, then you can do so by adding this code into your theme’s functions.php

[php]
add_action( ‘pre_get_posts’, ‘add_my_post_types_to_query’ );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( ‘post_type’, array( ‘post’, ‘portfolio’ ) );
return $query;
}
[/php]

Display using WP_Query custom post type

You can display on your specific page in page template. Then you can follow the code below:

[php]
<?php
$args = array( ‘post_type’ => ‘portfolio’, ‘posts_per_page’ => 10 );
$query= new WP_Query($args);

if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
<?php endif; ?>
[/php]

If you don’t like the code you can use the plugin. I recommend this plugin for you. Custom Post Type UI This plugin provides an easy to use interface to create and administer custom post types and taxonomies in WordPress. This plugin is created for WordPress 3.x.

What do you think?

Written by Sopheap

The goal of me is sharing new ideas, inspiration art, fresh and useful resource, covering web design and development, graphic design, advertising, mobile development and life style.

Comments

Leave a Reply

Your email address will not be published.

Display Breadcrumbs To Your WordPress Website ~ Sopheap

Display Breadcrumbs To Your WordPress Website

Create Slide Show In WordPress

Create Slide Show In WordPress