Support » Plugins and Hacks » Facetious » Displaying taxonomy filters hierarchically

  • Hey everyone,

    First of all, awesome plugin! Unfortunately I’m struggling a bit with some customization and wanted to post this just to see if anyone had any thoughts.

    Right now Facetious just lists taxonomy filters all one on top of another, seemingly ignoring hierarchy.

    Is it possible to display taxonomy filter dropdowns hierarchically, similar to how WordPress’ own wp_dropdown_categories() function handles it?

    I tried stealing some of the code related to the function mentioned above but haven’t had any luck so far in making it work with Facetious.

    Any ideas would be greatly appreciated.

    http://wordpress.org/plugins/facetious/

Viewing 7 replies - 1 through 7 (of 7 total)
  • function5

    (@function5)


    I thought I would add to this as I think my question adds to this. Is it possible to manually sort a taxonomy? Currently it just lists the taxonomy terms alphabetically.

    I made the category drop-down hierarchical, with counts for categories and tags, with these mods to template.php:

    Look for:
    # Taxonomy dropdown:

    Replace the $terms = get_terms… with:

    $terms = get_terms( $key, array(
    	'hide_empty' => true,
    	'pad_counts' => true
    ) );
    sort_terms_hierarchy($terms);

    Then add this sort_terms_hierarchy function somewhere:

    function sort_terms_hierarchy(Array &$terms) {
    	$result = array();
    	$parent = 0;
    	$depth = 0;
    	$i = 0;
    	do {
    		$temp = array();
    		foreach($terms as $j => $term) {
    			if ($term->parent == $parent) {
    				$term->depth = $depth;
    				$term->name = str_repeat(' ',$depth*4).$term->name.' ('.$term->count.')';
    				array_push($temp, $term);
    				unset($terms[$j]);
    			}
    		}
    		array_splice($result, $i, 0, $temp);
    		$parent = $result[$i]->term_id;
    		$depth = $result[$i]->depth + 1;
    	} while ($i++ < count($result));
    	$terms = $result;
    }

    Hope somebody can help me out here – I tried tbird12’s solution & it works fine! (thank you very much!)

    What I wanted to do is to indent the sub-taxonomies down the list, like:

    tax_1
    – sub_1
    – sub_2
    tax2
    – sub_3

    (and btw: that should be a feature!)
    so I started playing around with it. this almost did the job:

    function sort_terms_hierarchy(Array &$terms) {
    	$result = array();
    	$parent = 0;
    	$depth = 0;
    	$i = 0;
    	do {
    		$temp = array();
    		foreach($terms as $j => $term) {
    			if ($term->parent == $parent) {
    				$term->depth = $depth;
    				$term->name = str_repeat(' ',$depth).$term->name.' ('.$term->count.')';
    				array_push($temp, $term);
    				unset($terms[$j]);
    			}
    			elseif ($term->parent != $parent) {
    				$term->name = ('-&nbsp;').$term->name;
    			}
    		}
    		array_splice($result, $i, 0, $temp);
    		$parent = $result[$i]->term_id;
    		$depth = $result[$i]->depth + 1;
    	} while ($i++ < count($result));
    	$terms = $result;
    }

    … the only flaw is that with every taxonomy down the list, it will add 4 more – – – – to every sub-tax;
    (moving the array_push and the unset underneath my elseif will lead to the desired layout, but the items will be sorted alphabetically again)

    – what am I missing? any suggestion is appreciated!

    and yes, my php is very basic indeed 😉

    Ted,

    Whatever character you want for the indent, put in place of the ‘ ‘ in this line in my code:
    $term->name = str_repeat(‘ ‘,$depth*4).$term->name.’ (‘.$term->count.’)’;

    If you want a dash, use:
    $term->name = str_repeat(‘-‘,$depth*4).$term->name.’ (‘.$term->count.’)’;

    oh wow, I didnt expect such a quick response - im flabbergasted, thank you so much!

    if I insert an &nbsp it looks well organized already (so I could live with that), but if I insert a dash it still gets repeated 4 times (but doesnt stack anymore) can you tell me whats the reason for that?

    Does this do what you want?:

    $term->name = str_repeat('-&nbsp;',$depth).$term->name.' ('.$term->count.')';

    But, that won’t work if you have sub-sub cats, where you just want one dash even when a cat is 2 or more deep.

    Oh! My original code should have &-n-b-s-p-; where it shows a space. But, it doesn’t show. I just noticed that.

    perfect! that did the trick, thank you so much!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying taxonomy filters hierarchically’ is closed to new replies.