Wednesday, 18 June 2014

Custom post type having same taxonomy in wordpress

Register custom post type and taxonomy

// function.php code

register_post_type('test1',
                      array(
                        'labels' => array(
                                     'name' => _x('Test1'),
                                     'singular_name' => _x('Test1'),
                                     'add_new' => _x('Add Test1', 'test1'),
                                     'add_new_item' => __('Add New Test1'),
                                     'edit_item' => __('Edit Test1'),
                                     'new_item' => __('New Test1'),
                                     'view_item' => __('View Test1'),
                                     'search_items' => __('Search Test1'),
                                     'not_found' =>  __('No Test1'),
                                     'not_found_in_trash' => __('No Test1 found in Trash'),
                                     'parent_item_colon' => ''
                                   ),
                        'public' => true,
                        'has_archive' => true,
                        'publicly_queryable' => true,
                        'query_var' => 'social_reporting',
                        'rewrite' => array('slug'=>'test1', 'with_front'=>false),
                        'capability_type' => 'post',
                        'hierarchical' => true,
'supports' => array('title', 'editor', 'page-attributes', 'thumbnail','taxonomy'),
'taxonomies' => array('post_tag')
                     
                 ));
 // custom post type for Video
  register_post_type('video',
                      array(
                        'labels' => array(
                                     'name' => _x('Video'),
                                     'singular_name' => _x('Video'),
                                     'add_new' => _x('Add Video', 'video'),
                                     'add_new_item' => __('Add New Video'),
                                     'edit_item' => __('Edit Video'),
                                     'new_item' => __('New Video'),
                                     'view_item' => __('View Video'),
                                     'search_items' => __('Search Video'),
                                     'not_found' =>  __('No Video found'),
                                     'not_found_in_trash' => __('No Video in Trash'),
                                     'parent_item_colon' => ''
                                   ),
                        'public' => true,
                        'has_archive' => true,
                        'publicly_queryable' => true,
                        'query_var' => 'video',
                        'rewrite' => array('slug'=>'video', 'with_front'=>false),
                        'capability_type' => 'post',
                        'hierarchical' => true,
'supports' => array('title', 'editor', 'page-attributes', 'thumbnail','taxonomy'),
'taxonomies' => array('post_tag')
                     
                 ));
}
add_action( 'init', 'initialize_autos_post_types' );
//custom taxonomy
register_taxonomy('pan_taxonomy', array('test1','video'),  array(
'label' => __('Add Categories'),
'singular_label' => __('Add Category'),
'hierarchical' => true,
'query_var' => 'pan_taxonomy',
'rewrite' => false,
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
        ));


To fetch custom taxonomy post 

// page.php code

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $post_type = array('test1','video');
        $loop = new WP_Query( array( 'post_type' => $post_type, 'posts_per_page' => -1, 'orderby'=>'date', 'paged'=>$paged ) );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
       <article>
           <div class="right_blog_post">
                        <?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
<div class="entry-content">
                         <?php
                         the_excerpt();
                         ?>
    </div>
                    </div>
                </article>
  <?php endwhile; ?>


No comments: