• Resolved aaronblomberg

    (@aaronblomberg)


    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://ww.wp.xz.cn/plugins/custom-post-type-ui/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    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

    (@aaronblomberg)

    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

    (@tw2113)

    The BenchPresser

    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

    (@aaronblomberg)

    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

    (@tw2113)

    The BenchPresser

    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.