Forums › Forums › Search & Filter Pro › Display multiple terms
Tagged: search terms
- This topic has 8 replies, 2 voices, and was last updated 6 years, 8 months ago by Anonymous.
-
Anonymous(Private) April 11, 2018 at 7:21 pm #171604
I’ve been crunching through the forum, but not seeing an answer.
– – – – – – – –
I’m using the following code to display a list of current terms for the filtered search:
</php global $searchandfilter; $sf_current_query = $searchandfilter->get(99801)->current_query()->get_array(); echo '<strong>Filters</strong>'; if ( empty($sf_current_query ) ) { echo ' • ' . "All" . "<br />"; } else { foreach($sf_current_query as $key) { echo ' • ' . $key['active_terms'][0]['name'] . ' '; } } echo "<br /><br />"; ?>
– – – – – – –
My filter includes dropdowns and checkboxes. For example:
Market (dropdown)
FloridaCuisines (checkboxes)
[ ] American
[X] Caribbean
[ ] Italian
[X] Mexican– – – – – – –
The code works and the list displays, but it only shows one of the selected checkbox terms…
Filters • Florida • Mexican
Instead of…
Filters • Florida • Caribbean • Mexican
OR.. Filters • Florida • Caribbean, MexicanI suspect that an array is required, but not sure what/where/how.
Please help this newbie out.
Thanks!
Trevor(Private) April 11, 2018 at 8:57 pm #171651You are correct. You need to examine the data structure of the filter array and then, where multiple values are permitted, use a loop to output them.
You can print the filter array to screen with code like this:
$sf_current_query = $searchandfilter->get(1446)->current_query(); echo '<pre>'; print_r($sf_current_query->get_array()); echo '</pre>';
But then it is a matter of coding it in PHP. I cannot see an example code snippet on our forum though. I would have thought it possible to replace echo with explode where you are handling an array?
Anonymous(Private) April 12, 2018 at 4:34 am #171733Okay, good news, I’m making progress…
This works for the array:
<?php global $searchandfilter; $sf_current_query = $searchandfilter->get(99801)->current_query(); if ( empty($sf_current_query ) ) { echo '<strong>Filters</strong>' . ' • ' . "All" . "<br /><br />"; } else { $args = array( "str" => '%2$s', "delim" => array(", ", " - "), "field_delim" => ', ', "show_all_if_empty" => false ); echo '<strong>Filters</strong>' . ' • ' . $sf_current_query->get_fields_html(array("_sft_cuisine", "_sft_name"), $args); } echo '<br />'; ?>
Now I need to pull in the $key[‘active_terms’][0][‘name’] from the original code (it will display before the array).
That will give me what I’m after…
Filters • Florida • Caribbean, MexicanHow do I combine it?
Anonymous(Private) April 12, 2018 at 11:07 pm #171986Trevor, thanks again for spending time with me today. I really appreciated it.
Good news, I finally figured it out!
Here’s the happy code:
<?php global $searchandfilter; $args = array( "str" => '%2$s', "delim" => array(", ", " - "), "field_delim" => ', ', "show_all_if_empty" => false ); $sf_current_query = $searchandfilter->get(99801)->current_query()->get_array(); $sf_current_style_query = $searchandfilter->get(99801)->current_query()->get_fields_html(array("_sft_style", "_sft_name"),$args); $sf_current_cuisine_query = $searchandfilter->get(99801)->current_query()->get_fields_html(array("_sft_cuisine", "_sft_name"),$args); echo '<strong>Filters</strong>'; if ( empty($sf_current_query ) ) { echo ' • ' . "All" . "<br /><br />"; } else { echo ' • ' . $sf_current_query['_sft_category']['active_terms'][0]['name'] . ' • ' . $sf_current_query['_sfm_wpcf-location-city']['active_terms'][0]['name'] . ' • ' . $sf_current_style_query . ' • ' . $sf_current_cuisine_query; echo "<br /><br />"; } ?>
Cheers!
-
AuthorPosts