Title: Add post category class
Last modified: August 22, 2016

---

# Add post category class

 *  [Hadhops](https://wordpress.org/support/users/hadhops/)
 * (@hadhops)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/)
 * Hello,
    I’m trying to customize the ajax results using the filter from your site,
   but my php knowledge is low. I want to add the post category to each result as
   a class. Then show the title, excerpt and meta.
 * Here is where I’m at:
 * ‘add_filter(‘uwpqsf_result_tempt’, ‘customize_output’, ”, 4);
    function customize_output(
   $results , $arg, $id, $getdata ){ // The Query $apiclass = new uwpqsfprocess();
   $query = new WP_Query( $arg ); ob_start(); $result = ”; // The Loop
 *  if ( $query->have_posts() ) {
    while ( $query->have_posts() ) { $query->the_post();
   global $post; echo ‘<div class=”client-box” WOULD LIKE THE CATEGORY NAME HERE
   >’; echo ‘<h1 class=”client-title”>’.get_the_title().'</h1>’; echo ‘<h2 class
   =”client-excerpt”>’.get_the_excerpt().'</h2>’; echo ‘<p>’.the_meta().'</p>’; 
   echo ‘</div>’; } echo $apiclass->ajax_pagination($arg[‘paged’],$query->max_num_pages,
   4, $id, $getdata); } else { echo ‘No clients fit these criteria’; } /* Restore
   original Post Data */ wp_reset_postdata();
 *  $results = ob_get_clean();
    return $results; }’
 * Thanks!!
 * [https://wordpress.org/plugins/ultimate-wp-query-search-filter/](https://wordpress.org/plugins/ultimate-wp-query-search-filter/)

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

 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306786)
 * You can use the [wp_get_object_term](http://codex.wordpress.org/Function_Reference/wp_get_object_terms)
   to get the taxonomy terms.
 *  Thread Starter [Hadhops](https://wordpress.org/support/users/hadhops/)
 * (@hadhops)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306847)
 * Thanks for that TC.K,
    I’ve had another try but still can’t get the category 
   name to appear as a class. Do you mind having a look at where I’m going wrong?
 * Thanks again!
 *     ```
       add_filter('uwpqsf_result_tempt', 'customize_output', '', 4);
       function customize_output($results , $arg, $id, $getdata ){
       	 // The Query
                   $apiclass = new uwpqsfprocess();
                    $query = new WP_Query( $arg );
       		ob_start();	$result = '';
       			// The Loop
   
       			if ( $query->have_posts() ) {
       			while ( $query->have_posts() ) {
       				$query->the_post();global $post; 
   
       									echo '<div class="client-box'; foreach(wp_get_object_terms() as $taxo) { echo 'category-'.$taxo->slug; }; echo '">';
                                       	echo '<span>'.get_the_post_thumbnail().'</span>';
       	                                echo '<h1 class="client-title">'.get_the_title().'</h1>';
       	                                echo '<h1 class="client-excerpt">'.get_the_excerpt().'</h1>';
       	                                echo '<h1>'.the_meta().'</h1>';
                                       	echo '</div>';
       			}
                               echo  $apiclass->ajax_pagination($arg['paged'],$query->max_num_pages, 4, $id, $getdata);
       		 } else {
       					 echo  'No clients fit these criteria';
       				}
       				/* Restore original Post Data */
       				wp_reset_postdata();
   
       		$results = ob_get_clean();
       			return $results;
       }
       ```
   
 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306850)
 * First, your wp_get_object_terms() doesn’t have the proper arguments.
    Secondly
   don’t directly insert the wp_get_object_terms() in the the foreach loop directly.
 * So the better ways is:
 *     ```
       $catenames = wp_get_object_terms($post->ID, 'YOUR_TAXONOMY_SLUG');
   
       echo '<div class="client-box'; foreach($catenames as $taxo) { echo 'category-'.$taxo->slug; }; echo '">';
       ```
   
 * If I am right, you inserting the category classes for sorting purpose, right?
   Using plugin like jquery isotope? If so, this will NOT WORK. It is an Ajax generated
   DOM, it will not blind the event when the result is load.
    You either using the
   js [delegate method](http://stackoverflow.com/a/8111171/1310740) or include the
   script on the function above before the loop is calling. If you don’t know how
   to do it, then using the default result template instead.
 *  Thread Starter [Hadhops](https://wordpress.org/support/users/hadhops/)
 * (@hadhops)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306851)
 * Thank you,
 * All of the sorting options in the plugin are fantastic, so everything is functioning
   perfectly on load.
 * The reason I want the category name added as a class is so I can style the results
   based on their category.
 * Does that make a difference to your recommendation?
 *  Plugin Author [TC.K](https://wordpress.org/support/users/wp_dummy/)
 * (@wp_dummy)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306854)
 * If you solely want styling, then go ahead. But the js effect will not work on
   Ajax searched results though.
 *  Thread Starter [Hadhops](https://wordpress.org/support/users/hadhops/)
 * (@hadhops)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306857)
 * Cool, thanks again.
 * Your plugin support is fantastic!

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

The topic ‘Add post category class’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/ultimate-wp-query-search-filter_fcfcfc.
   svg)
 * [Ultimate WP Query Search Filter](https://wordpress.org/plugins/ultimate-wp-query-search-filter/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ultimate-wp-query-search-filter/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/)
 * [Active Topics](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ultimate-wp-query-search-filter/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [Hadhops](https://wordpress.org/support/users/hadhops/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/add-post-category-class/#post-5306857)
 * Status: not resolved