• 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/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author TC.K

    (@wp_dummy)

    You can use the wp_get_object_term to get the taxonomy terms.

    Thread Starter Hadhops

    (@hadhops)

    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

    (@wp_dummy)

    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 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

    (@hadhops)

    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

    (@wp_dummy)

    If you solely want styling, then go ahead. But the js effect will not work on Ajax searched results though.

    Thread Starter Hadhops

    (@hadhops)

    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.