• Hey guys,

    Ive created custom posts on a wordpress theme i have developed but I find the way I do it rather in-effcient and at times buggy. Ive created all my custom posts through one function. My code is below:

    /* Custom Post Types*/
    
    /*News Post Type*/
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( 'news_article',
        array(
          'labels' => array(
            'name' => __( 'News' ),
            'singular_name' => __( 'Article' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'news_article'),
          'supports' => array('thumbnail', 'editor','title', 'excerpt', 'editor', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
    
      );
    
      register_post_type( 'bnf',
        array(
          'labels' => array(
            'name' => __( 'Business and Finances' ),
            'singular_name' => __( 'Business and Finance' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'bnf'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
        register_post_type( 'events',
        array(
          'labels' => array(
            'name' => __( 'Events' ),
            'singular_name' => __( 'Event' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'events'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
      register_post_type( 'videos',
        array(
          'labels' => array(
            'name' => __( 'Videos' ),
            'singular_name' => __( 'Video' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'videos'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
      register_post_type( 'products',
        array(
          'labels' => array(
            'name' => __( 'Products' ),
            'singular_name' => __( 'Product' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'products'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
      register_post_type( 'sponsors',
        array(
          'labels' => array(
            'name' => __( 'Sponsors' ),
            'singular_name' => __( 'Sponsor' ),
    
          ),
          'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'sponsors'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
        register_post_type( 'projects',
        array(
          'labels' => array(
            'name' => __( 'Projects' ),
            'singular_name' => __( 'Project' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'projects'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
      register_post_type( 'opinions',
        array(
          'labels' => array(
            'name' => __( 'Opinions' ),
            'singular_name' => __( 'Opinion' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'opinions'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
    
      register_post_type( 'tenders',
        array(
          'labels' => array(
            'name' => __( 'Tenders' ),
            'singular_name' => __( 'Tender' ),
    
          ),
    'taxonomies' => array('category','post_tag'),
          'public' => true,
          'has_archive' => true,
          'rewrite' => array('slug' => 'tenders'),
          'supports' => array('thumbnail', 'editor','title', 'editor', 'excerpt', 'author', 'thumbnail',
    				  					  'comments', 'revisions', 'custom-fields',),
        )
      );
        flush_rewrite_rules();
      }

    Is this the best way to do it? Are there more safer and efficient ways to do it? When I switch the debugger on wordpress it doesn’t say there are errors

    Thanks in advance

  • The topic ‘Creating custo post types’ is closed to new replies.