Custom post type in WordPress. Part I

Why we need Custom Post Type?

We use it in situation when standard posts and pages are not enough for a project.

For example in a website for vector artist I used it for Projects and for About page. It take me more flexible instrument for managing content.

Every horisontal part on this page is a single post. So when I need add or delete some block I can just create or delete custom post.

Custom post type is not everything what I used. But now I’m prefer talking only about post types.

For new post type I added this code in function.php:

 function about_posttype(){
   register_post_type(
     'about',
     array(
       'labels' => array(
       'name' => __( 'About' ),
       'singular_name' => __( 'About' ),
       'all_items' => __( ' All Blocks' ),
       'add_new' => __( 'Add New Block' ),
       'add_new_item' => __( 'Add New Block' ),
       'edit_item' => __( 'Edit Block' ),
       'new_item' => __( 'Add New Block' ),
       'view_item' => __( 'View Block' ),
       'search_items' => __( 'Search Block' ),
       'not_found' => __( 'No projects found' ),
       'not_found_in_trash' => __( 'No projects found in trash' ),
     ),
     'public' => true,
     'capability_type' => 'post',
     'menu_position' => 3,
     'supports' => array('title', 'editor', 'thumbnail'),
     '_builtin' => false,
     'rewrite' => array('slug' => 'about')
     )
  );
}
add_action ('init', 'about_posttype');

For output content of custom post type I use this code:

<?php if ( have_posts() ) :
  while ( have_posts() ) : the_post();
    the_content(); 
  endwhile;
endif; ?>

Also I created page template for About page:

<?php
/**
 * Template Name: about
 */

get_header(); ?>
  <div id="about" class="about" role="about">
    <?php
      query_posts(array('post_type' => 'about', 'posts_per_page' => -1));
    ?>
    <?php get_template_part('loop-about');
    <?php wp_reset_query(); ?>
 </div>
<?php get_footer("about"); ?>

Next time more about Custom Post Type.