Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create custom post type in WordPress

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 466
    Comment on it

    Custom posts type is used when we are making a new theme from html or want to add some extra feild in the made theme.Suppose we want to have a separate section for Trending News. By using Custom Post Types we can create a new custom news type of item like Posts and Pages, which will contain a different set of data.
    This custom post type will have a new  menu,  editing pages, and all.

    Custom Post Types are a new set of options that appearing withpost types such as Posts, Pages. A Custom Post Type can store any type of information. It has its own media uploader and uses the existing WordPress table structure.
    We can make the custom posts type in our wordpress by using the code below in our current wordpress's theme function.php

    add_action( 'init', 'codex_news_init' ); //execute the custom function 
    function codex_news_init() {
    	$labels = array(
    		'name'               => _x( 'News', 'post type general name', 'your-plugin-textdomain' ),
    		'singular_name'      => _x( 'News', 'post type singular name', 'your-plugin-textdomain' ),
    		'menu_name'          => _x( 'News', 'admin menu', 'your-plugin-textdomain' ),
    		'name_admin_bar'     => _x( 'News', 'add new on admin bar', 'your-plugin-textdomain' ),
    		'add_new'            => _x( 'Add New', 'news', 'your-plugin-textdomain' ),
    		'add_new_item'       => __( 'Add New News', 'your-plugin-textdomain' ),
    		'new_item'           => __( 'New News', 'your-plugin-textdomain' ),
    		'edit_item'          => __( 'Edit News', 'your-plugin-textdomain' ),
    		'view_item'          => __( 'View News', 'your-plugin-textdomain' ),
    		'all_items'          => __( 'All News', 'your-plugin-textdomain' ),
    		'search_items'       => __( 'Search News', 'your-plugin-textdomain' ),
    		'parent_item_colon'  => __( 'Parent News:', 'your-plugin-textdomain' ),
    		'not_found'          => __( 'No news found.', 'your-plugin-textdomain' ),
    		'not_found_in_trash' => __( 'No news found in Trash.', 'your-plugin-textdomain' )
    	);
    
    	$args = array(
    		'labels'             => $labels,
            'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    		'public'             => true,//the visibility of the custom post type
    		'menu_icon' 		 => 'dashicons-images-alt', // shows the icon in admin panel
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array( 'slug' => 'news' ),
    		'capability_type'    => 'post',
    		'has_archive'        => true,//enables archiving of the custom post type
    		'hierarchical'       => false,
    		'menu_position'      => null, // we can define the menu position
    		'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) //the custom post type which is to be displayed.
    	);
    	register_post_type( 'news', $args );
    }

    we can also define the categories for the custom posts type we have add. By writing the below code we can also have categories for the custom post type. We can make a new category and can assign category for any posts we have made in custom post type.

    add_action( 'init', 'create_news_taxonomy', 0 );
    function create_news_taxonomy() {
     
      $training_args = array(
    	'name'                => _x( 'News Category ', 'taxonomy general name' ),
    	'singular_name'       => _x( 'News Category', 'taxonomy singular name' ),
    	'search_items'        => __( 'Search News Category' ),
    	'all_items'           => __( 'All News Category' ),
    	'parent_item'         => __( 'Parent News Category' ),
    	'parent_item_colon'   => __( 'Parent News Category:' ),
    	'edit_item'           => __( 'Edit News Category' ),
    	'update_item'         => __( 'Update News Category' ),
    	'add_new_item'        => __( 'Add New News Category' ),
    	'new_item_name'       => __( 'New News Category name' ),
    	'menu_name'           => __( 'News Category' )
      );    
    
     $taxonomy_args = array(
    	'hierarchical'        => true,
    	'labels'              => $training_args,
    	'show_ui'             => true,
    	'show_admin_column'   => true,
    	'query_var'           => true,
    	'rewrite'             => array( 'slug' => 'news-category' )
      );
    
      register_taxonomy( 'news-category', array( 'news' ), $taxonomy_args );
    }
    

    After defining the custom posts and its categories, we can call it in any of the wordpress template according to the requirements like this code below.

    <?php
    	$args = array(
    	'post_type' => 'news',
    	'posts_per_page' => 3
    	);
    	$query = new WP_Query($args);
    	if( $query->have_posts() ) {
            while( $query->have_posts() ) {
    	$query->the_post(); 
    	// custom content ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: