megwoo
6/24/2015 - 5:34 AM

WordPress Custom Post Type

http://wordpress.org/support/topic/add-category-to-a-custom-post-type
http://codex.wordpress.org/Post_Types


in functions:

function xxx_custom_init() {
    $args = array(
      'public' => true,
      'has_archive' => true,
      'label'  => 'Xxx',
      'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' )
    );
    register_post_type( 'xxx', $args );   // DO NOT USE dashes in post type name!
}
add_action( 'init', 'xxx_custom_init' );

// post types: https://codex.wordpress.org/Function_Reference/post_type_supports

**** must reset permalinks through settings ****

then archives can be accessed via http://site.com/blog_folder/xxx

if custom template is desired, templates need to be named as follows:
archive-xxx.php (archive listing)
single-xxx.php (single post)


in functions:

// set  number of posts per page for custom post type
function custom_posts_per_page($query) {
    switch ( $query->query_vars['post_type'] )
    {
        case 'iti_cpt_1':  // Post Type named 'iti_cpt_1'
            $query->query_vars['posts_per_page'] = 3;
            break;

        case 'iti_cpt_2':  // Post Type named 'iti_cpt_2'
            $query->query_vars['posts_per_page'] = 4;
            break;

        case 'iti_cpt_3':  // Post Type named 'iti_cpt_3'
            $query->query_vars['posts_per_page'] = 5;
            break;

        default:
            break;
    }
    return $query;
}
if( !is_admin() ) {
    add_filter( 'pre_get_posts', 'custom_posts_per_page' );
}



// enable tags for custom post types
register_taxonomy( 
    'xxx_tag', 
    'xxx',      // match custom post type slug
    array( 
        'hierarchical'  => false, 
        'label'         => __( 'Tags', CURRENT_THEME ), 
        'singular_name' => __( 'Tag', CURRENT_THEME ), 
        'rewrite'       => true, 
        'query_var'     => true 
    )  
);

// then to get all tags
$terms = get_terms( 'xxx_tag' );