Friday 24 June 2016

How to Create Custom Post Type ( CPT ) in WordPress?

There is Some good plugin available for Custom Post Type.
"Custom Post Type UI" Plugin can download from "https://wordpress.org/plugins/custom-post-type-ui/" URL.



For Manually Create "Custom Post Type" (CPT) follow the following Steps.
1> Create CUSTOM POST TYPE  (Add Fol1owing code to "functions.php" file or Plugin file)
<?php
 // Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

// Our custom post type function. Here, "movies" is our new Custom Post Type.
function create_posttype() {
register_post_type( 'movies',
// CPT Options
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'movies'),
)
);
}



/* Creating a function to create our Custom Post Type
 * Text Domain: twentysixteen in "style.css" file you get it.
 */

function custom_post_type() {

// Set UI labels for Custom Post Type
$labels = array(
'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name'           => __( 'Movies', 'twentythirteen' ),
'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
'all_items'           => __( 'All Movies', 'twentythirteen' ),
'view_item'           => __( 'View Movie', 'twentythirteen' ),
'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
'add_new'             => __( 'Add New', 'twentythirteen' ),
'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
'update_item'         => __( 'Update Movie', 'twentythirteen' ),
'search_items'        => __( 'Search Movie', 'twentythirteen' ),
'not_found'           => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
);


// Set other options for Custom Post Type
$args = array(
'label'               => __( 'movies', 'twentythirteen' ),
'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
'labels'              => $labels,
// Features this CPT supports in Post Editor
'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies'          => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'capability_type'     => 'page',
);

// Registering your Custom Post Type
register_post_type( 'movies', $args );
}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/

add_action( 'init', 'custom_post_type', 0 );
?>




2> For watch in front Custom Post Type

 If you are using SEO friendly permalinks then
 http://example.com/movies
 Else
 http://example.com/?post_type=movies

If you want to change the output look of archive page then,
Copy & Paste "archive.php" theme file.
Then rename it "archive-{posttype}.php". Like "archive-movies.php".
sample code add in "archive-movies.php" file
<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile; endif;
get_footer();
?>


 If you want to change the output look of post page then,
 Copy & Paste "single.php" theme file.
 Then rename it "single-{posttype}.php". Like "single-movies.php".




3> Displaying Custom Post Types on The Front Page (add following code in "functions.php" or plugin file)

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', 'movies' ) );
return $query;
}


4> For Querying Custom Post Types
Example Code:-
<?php
$args = array( 'post_type' => 'movies', '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 wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>


5> For Displaying Custom Post Types in Widgets
First thing you need to do is install and activate the "Ultimate Posts Widget" plugin. Upon activation, simply go to Appearance » Widgets and drag and drop the Ultimate Posts widget to a sidebar.


For more Interesting, Useful Article & codes visit IT New Code.

Ankit Shah PHP Expert

IT New Code suggest a solution for your problem related to coding, Specially .PHP, Wordpress, WooCommerce, Magento, HTML and CSS. Visit Our website for Information.

0 comments:

Post a Comment

 

Copyright @ 2016 IT New Code | Developing Code | Designing Code.

Designed by: Ankit Shah