• Hi! I need to modify your plugin little bit so here is my question – is it possible to add kategory title before each category listing like this

    Category 1
    Store 1
    Store 2
    Store 3
    Category 2
    Store 4
    Store 5
    Store 6
    Need to know just where to dig to make it
    thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Tijmen Smit

    (@tijmensmit)

    This article doesn’t do exactly what you want, but it should help you get started. It shows how to include the names of the categories in each result. Maybe you don’t want to show them in each result, but you do need to get the category values to make sure they are grouped together.

    You will need to change the sort order as well, and make it by category.

    I think the part where you show the category title before a group of locations that fall in that category is best done with JS. After you have changed the sorting, and the search results are shown, you can use ajaxComplete to run JS code directly after the results are returned.

    You can then grab the first item of each category set, take the terms name and insert it before the first items in the category group as the header. Maybe this can be done in another way, but this is the first thing that came to my mind.

    Thread Starter umeeshh

    (@umeeshh)

    Hi! Thanks for your help. I’ve made it!

    Here is solution – not perfect one, but works:

    Adding code to function.php of template width sorting by terms (category) as you’ve mentioned:

    // custom sorting
    
    add_filter( 'wpsl_store_meta', 'custom_store_meta', 10, 2 );
    
    function custom_store_meta( $store_meta, $store_id ) {
        
        $terms = wp_get_post_terms( $store_id, 'wpsl_store_category' );
        
        $store_meta['terms'] = '';
        
        if ( $terms ) {
            if ( !is_wp_error( $terms ) ) {
                if ( count( $terms ) > 1 ) {
                    $location_terms = array();
    
                    foreach ( $terms as $term ) {
                        $location_terms[] = $term->name;
                    }
    
                    $store_meta['terms'] = implode( ', ', $location_terms );
                } else {
                    $store_meta['terms'] = $terms[0]->name;    
                }
            }
        }
        
        return $store_meta;
    }
    
    // custom store meta
    
    add_filter( 'wpsl_store_data', 'custom_result_sort' );
    
    function custom_result_sort( $store_meta ) {
        
        $custom_sort = array();
        
        foreach ( $store_meta as $key => $row ) {
            $custom_sort[$key] = $row['terms'];
        }
    
        array_multisort( $custom_sort, SORT_ASC, SORT_REGULAR, $store_meta );
        
        return $store_meta;
    }

    Add class of term in listing:

    <script id="wpsl-listing-template" type="text/template">
        <?php
            $listing_template = '<li class=" <%= terms %>" data-store-id="<%= id %>">' . "\r\n";

    ajaxComplete function which adds line of title code before each group of term locations:

    $( document ).ajaxComplete(function() {
    	$("#wpsl-stores ul li").each(function(){
    	var className = $(this).attr('class');
    	className = className.replace(/\s+/g, '');
    	$( "<div class='wpsl-stores-cityname "+ className +"'><p>" + className + "</p></div>" ).insertBefore( "#wpsl-stores ul li."+className + ":first" );
    	
    	$("#wpsl-stores ul .wpsl-stores-cityname."+className+":first").addClass("firstofname");
    	$("#wpsl-stores ul .wpsl-stores-cityname:not(.firstofname)").remove();
    	});
    
    });

    It adds as many title lines as locations number in term so I’ve needed to add class to first one and remove those without – maybe it can be done in simpler way ??

    Thanks again

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category Name’ is closed to new replies.