Wednesday, 16 December 2015

how to create and display categories for post type in WordPress

/******services_posttype***********/
function args_ourwork_posttype(){
   $args_services = array(
   'labels'=> array( 'name'=>'ourwork Tab',
       'singular_name'=> 'ourwork',
       'menu_name'=>'ourwork',
       'name_admin_bar'=> 'ourwork',
       'all_items' =>'View all ourwork',
       'add_new'=> 'Add New ourwork' ),
   'description' =>"This post type is for ourwork",
   'public' => true,
   'exclude_from_search'=>false,
   'publicly_queryable'=> true,
   'show_ui' => true,
   'show_in_menu'=> true,
   'show_in_admin_bar'=> true,
   'menu_position'=>6,
   'capability_type'=> 'page',
   'menu_icon' => get_stylesheet_directory_uri().'/images/dash-logos.png',
   'supports'=> array( 'title', 'editor', 'author', 'thumbnail', 'excerpt',
    ),
   'query_var'=>true,
  );

register_post_type( "ourwork", $args_services );
    $categories_labels = array(
        'label' => 'ourwork Category',
        'hierarchical' => true,
        'query_var' => true
    );
    register_taxonomy('ourwork_categories', 'ourwork', $categories_labels);

 }

 add_action("init","args_ourwork_posttype");
/******./args_services_posttype**********/








==========================







<?php

 $args = array(
 'post_type'=> 'ourwork',
 'tax_query' => array(
  array(
   'taxonomy' => 'ourwork_categories',
   'field' => 'slug',
   'terms' => 'all'
  )
 )
);

$my_query = new WP_Query($args);
//$postslist = get_posts( $args_args );
//var_dump($postslist);

//exit;
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
           

<div class="tab-img col-sm-3">
                 <?php the_post_thumbnail('full'); ?>
  <div class="tab-hover text-center">
  <img src="<?php echo get_stylesheet_directory_uri();?>/images/eyehover.png">
  <div class="link-hover"><?php the_content();?></div>
  </div>
</div>

                   <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
       

No comments:

Post a Comment

Alternative content