Title: Adding different classes to custom post type
Last modified: August 30, 2016

---

# Adding different classes to custom post type

 *  Resolved [aaronblomberg](https://wordpress.org/support/users/aaronblomberg/)
 * (@aaronblomberg)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/)
 * I was wondering if it was possible to define a class within each custom post 
   type entry?
 * I created a new custom post type and its corresponding taxonomy and they are 
   pulling into the site just fine. I have them displayed using and unordered list
   and i was wondering if you could custom define the class of each entry so the
   < li > of each post would contain the class of the post. The reason is i need
   custom icons to match category they are within that custom post type.
 * For example here is my custom post type for “Chamber News”
 *     ```
       <?php
       	$custom_post_type = 'chamber-news';
       	$args=array(
       	  'post_type' => $custom_post_type,
       	  'post_status' => 'publish',
       	  'posts_per_page' => 5,
       	  'caller_get_posts'=> 1
       	);
       	$my_query = null;
       	$my_query = new WP_Query($args);
       	if( $my_query->have_posts() ) {
       	  // echo 'List of Chamber News';
       	  while ($my_query->have_posts()) : $my_query->the_post(); ?>
       	    <ul class="chamber-news-preview">
       	    	<li>
       		    	<?php the_content(); ?>
       		    </li>
       		</ul>
       	    <?php
       	  endwhile;
       	}
       	wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 * But, I’d like to tag the < li > with a class such as: “community”, “event”, “
   public”, and “private”. Is there anyway to accomplish this? Thank you so much
   in advance!!
 * [https://wordpress.org/plugins/custom-post-type-ui/](https://wordpress.org/plugins/custom-post-type-ui/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/#post-6578750)
 * I’d just use a custom field, since you’re putting the post type posts into the`
   <li>` items. Just fetch the meta value and append it as a class attribute.
 *  Thread Starter [aaronblomberg](https://wordpress.org/support/users/aaronblomberg/)
 * (@aaronblomberg)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/#post-6578889)
 * thanks Michael – I gave that a shot but to no avail… On the custom post itself
   in the custom fields i added a name of “chamber-news-icon” and a value of “community”.
   In the page template itself i added that to the < li > here:
 *     ```
       <?php
       	$custom_post_type = 'chamber-news';
       	$args=array(
       	  'post_type' => $custom_post_type,
       	  'post_status' => 'publish',
       	  'posts_per_page' => 5,
       	  'caller_get_posts'=> 1
       	);
       	$my_query = null;
       	$my_query = new WP_Query($args);
       	if( $my_query->have_posts() ) {
       	  // echo 'List of Chamber News';
       	  while ($my_query->have_posts()) : $my_query->the_post(); ?>
       	    <ul class="chamber-news-preview">
       	    	<li class="<?php get_post_meta($postid, 'chamber-news-icon', true); ?>">
       		    	<?php the_content(); ?>
   
       	    <?php
       	  endwhile;
       	}
       	wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 * unfortuantely, the result just returns:
 *     ```
       <li class="">
       ```
   
 * any further insight would be GREATLY appreciated. thank you SO much!!
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/#post-6578892)
 * You need to echo it.
 *     ```
       <li class="<?php echo get_post_meta($postid, 'chamber-news-icon', true); ?>">
       ```
   
 * Should also be able to use `get_the_ID()` instead of `$postid`
 *  Thread Starter [aaronblomberg](https://wordpress.org/support/users/aaronblomberg/)
 * (@aaronblomberg)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/#post-6578945)
 * Hi Michael,
 * Just wanted to let you know this is what ended up working for me:
 *     ```
       <?php
       	$custom_post_type = 'chamber-news';
       	$args=array(
       	  'post_type' => $custom_post_type,
       	  'post_status' => 'publish',
       	  'posts_per_page' => 5,
       	  'caller_get_posts'=> 1
       	);
       	$my_query = null;
       	$my_query = new WP_Query($args);
       	if( $my_query->have_posts() ) {
       	  // echo 'List of Chamber News';
       	  while ($my_query->have_posts()) : $my_query->the_post(); ?>
       	    <ul class="chamber-news-preview">
       	    	<li class="<?php echo get_post_meta($post->ID, 'chamber-news-icon', true); ?>">
   
       		    	<?php the_content(); ?>
       		    </li>
       		</ul>
       	    <?php
       	  endwhile;
       	}
       	wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 * Thanks for your help!
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/#post-6578946)
 * You’re welcome. Glad you got a solution figured out.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Adding different classes to custom post type’ is closed to new replies.

 * ![](https://ps.w.org/custom-post-type-ui/assets/icon-256x256.png?rev=2744389)
 * [Custom Post Type UI](https://wordpress.org/plugins/custom-post-type-ui/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/custom-post-type-ui/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/custom-post-type-ui/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-post-type-ui/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-post-type-ui/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-post-type-ui/reviews/)

## Tags

 * [class](https://wordpress.org/support/topic-tag/class/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)

 * 5 replies
 * 2 participants
 * Last reply from: [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/adding-different-classes-to-custom-post-type/#post-6578946)
 * Status: resolved