Custom post type in WordPress

Custom Post Type (CPT) in wordpress is very interesting facts to achieve what ever you want to implement in your website that developed base on WordPress Platform. instead of adding your specific category in default wordpress post type you may create your own custom post type and display it with SEO friendly url with category wise.

custom post type in wordpress

Example : For News section you want to set up separately from WordPress default post type with various news category than Register Custom Post type cst_new in WordPress by adding following code in theme functions.php file

#
 array(
				'name' => 'News',
				'singular_name' => 'news'
			),
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array('slug' => 'news','with_front' => false),
			'supports'    => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'post-formats'),
			'can_export'  => true,
		)
	);
register_taxonomy('news_cat', 'cst_new', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' =>array( 'slug' => 'news' )));
register_taxonomy('news_tag', 'cst_new', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));
}

?>

If you want to insert category slug of custom post type taxonomy between custom post type and post

Update above code with following :

#
 array(
				'name' => 'News',
				'singular_name' => 'news'
			),
			'public'      => true,
			'has_archive' => true,
			'rewrite'     => array('slug' => 'news/%cat_news%','with_front' => false),
			'supports'    => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'page-attributes', 'post-formats'),
			'can_export'  => true,
		)
	);
register_taxonomy('news_cat', 'cst_new', array('hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => array( 'slug' => 'news' )));
register_taxonomy('news_tag', 'cst_new', array('hierarchical' => false, 'label' => 'Tags', 'query_var' => true, 'rewrite' => true));
}

add_filter('post_link', 'news_permalink', 1, 3);
add_filter('post_type_link', 'news_permalink', 1, 3);

function news_permalink($permalink, $post_id, $leavename) {
	//replace %cat_news% with custom post type category slug
    if (strpos($permalink, '%cat_news%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
       $terms = wp_get_object_terms($post->ID, 'cat_news');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
        	$taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'default-slug';

    return str_replace('%cat_news%', $taxonomy_slug, $permalink);
}	
?>

Ref : https://wordpress.org/support/topic/insert-category-of-taxonomy-between-custom-post-type-and-post

To Display custom post type in website front end create template page with your theme default template page copy paste code and change it default wp_query to

#
 'cst_new','post_status' => 'publish','paged' => $paged, 'posts_per_page' => 5 );
$wp_query = new WP_Query( $args );

// to Display Pagination page list 
if ($wp_query->max_num_pages > 1) 
// call theme pagination function like twentyfourteen_paging_nav() or tie_pagenavi(); etc

Create Archive page for Custom post type copy the code from your theme’s archive.php file and paste it in your new file that created with name archive-{customposttype}.php file. Then add style to design it according to your heart’s desire.
A very basic archive file would look like this:

#
';
'; the_content(); echo '
'; endwhile; endif; get_footer(); ?>

Note** : to avoid slug conflict issue (page not found ) with custom post type set different different slug for custom post type slug, cpt category slug, cpt type slug.

Also while you create page with your newly created template file for display custom post type post choose different slug that does not used for registered Custom post type, cpt category and cpt tag.

In above example we used same News slug for register custom post type & Category so it throw Not found error while click on pagination link

Leave a Comment

Scroll to Top