0

I hope this question doesn't make me look too stupid!

I am using a plugin called 'Query Multiple Taxonomies' -- it's a fantastic plugin and is written extraordinarily well. http://wordpress.org/plugins/query-multiple-taxonomies/

It does just about everything that I need it to do save one small thing. I've been trying to figure out how to accomplish this last bit for a while now, and I decided that it was time to ask for some help.

You can view the section I'm working on here: http://tunagaming.dyndns.org/?section=tutorials

The drill-down works very well. This is how it is currently set up:

  • First page shows platforms.
  • Second page shows genres
  • Third page shows sub-genres.

This is how I want it to work, except that each of those are custom taxonomies, and I would like to eliminate 'sub-genres' completely in favor of a hierarchical 'genres' taxonomy.

So ultimately, I would like for the drill-down to work like this:

  • First page shows platforms.
  • Second page shows ONLY parent terms in the taxonomy 'genres'
  • Third page shows children of selected parent.

All of this is being called in an archive.php file. Here is the code for reference:

<?php if (is_multitax( array('section', 'platforms', 'genre') ) ){

the_widget('Taxonomy_Drill_Down_Widget', array(
    'title' => '',
    'mode' => 'lists',
    'taxonomies' => array('subgenre')
));?>
    <div id="postList">
    <h2>Query Results</h2>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <ul><a title="<?php the_title();?>" href="<?php the_permalink();?>"><li>
        <span class="image"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></span>
        <span class="content">
        <h3><?php the_title();?></h3>
        <?php the_excerpt(); ?>
        </span>

    </ul></li></a>
    <?php endwhile; ?>
    </div>

<?php 
}
elseif (is_multitax( array('section', 'platforms') ) ) {

the_widget('Taxonomy_Drill_Down_Widget', array(
    'title' => '',
    'mode' => 'lists',
    'taxonomies' => array('genre')
));
?>

    <div id="postList">
    <h2>Query Results</h2>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <ul><a title="<?php the_title();?>" href="<?php the_permalink();?>"><li>
        <span class="image"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></span>
        <span class="content">
        <h3><?php the_title();?></h3>
        <?php the_excerpt(); ?>
        </span>

    </ul></li></a>
    <?php endwhile; ?>
    </div>
<?php
} 
else {
the_widget('Taxonomy_Drill_Down_Widget', array(
    'title' => '',
    'mode' => 'lists',
    'taxonomies' => array('platforms') // list of taxonomy names
));
?>

    <div id="postList">
    <h2>Latest Entries</h2>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <ul><a title="<?php the_title();?>" href="<?php the_permalink();?>"><li>
        <span class="image"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></span>
        <span class="content">
        <h3><?php the_title();?></h3>
        <?php the_excerpt(); ?>
        </span>

    </ul></li></a>
    <?php endwhile; ?>
    </div>
<?php } ?>

I would love some help in figuring this out, or even some fresh ideas on how to achieve this drill-down. I'm not asking for anyone to code it for me, but it is just a bit over my head. I'm okay with PHP -- not great. So a nudge in the right direction would also be very helpful!

Thanks in advance.

EDIT:

For what it's worth, I believe the answer lies in the plugin's walker.php file. I could be wrong, but here's the code I've been studying this morning:

function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {

        if ( !$element )
            return;

        $id_field = $this->db_fields['id'];

        $id = $element->$id_field;

        $child_output = '';

        // descend only when the depth is right and there are childrens for this element
        if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {

            foreach ( $children_elements[ $id ] as $child ) {
                $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $child_output );
            }

            unset( $children_elements[ $id ] );
        }

        $this->single_el( $output, $element, $depth, $child_output );
    }

    function single_el( &$output, $term, $depth, $child_output ) {
        $data = $this->specific_data( $term, $depth );

        $data = array_merge( $data, array(
            'term-name' => $term->name,
            'is-selected' => in_array( $term->slug, $this->selected_terms ) ? array(true) : false,
            'depth' => $depth,
        ) );

        if ( !empty( $child_output ) ) {
            $data['children']['child-list'] = $child_output;
        }

        $full_data = new QMT_Data_Container( $this->taxonomy, $term, $data );

        $output .= Taxonomy_Drill_Down_Widget::mustache_render( $this->walker_type . '-item.html', $full_data );
    }

    abstract function specific_data( $term, $depth );
}

3 Answers 3

1

I had done some work for my upcoming theme which shows category tree (Taxonomy drill down) from custom taxonomy. You can show it on taxonomy page or on category page.

if(! defined('TH_TAX_TYPE')) define('TH_TAX_TYPE','your-tax-type');

function is_decendent($term_id,$main_parent) {
   $terms= get_term_by('id', $term_id,TH_TAX_TYPE);
   $parent_term=  get_term_by('id', $terms->parent,TH_TAX_TYPE);
  $grand_parent_term=  get_term_by('id',$parent_term->parent,TH_TAX_TYPE);

if($terms || $grand_parent_term){
    if($grand_parent_term->term_id==$main_parent){
        return TRUE;
    }
  }
return FALSE;
}



 function category_tree() {
    global $term;
    $get_current_term_info= get_term_by('slug', $term,TH_TAX_TYPE);

$all_category=  get_terms(TH_TAX_TYPE);
$out_put='<ul class="category-tree">
  <li><a href="'.get_option('all_cat_link').'">'.__('All Files','themeshive').'</a></li>';
foreach ($all_category as $main_cat) {
    if($main_cat->parent=='0'){
        $out_put.='<li><a href="'.get_term_link($main_cat).'">'.$main_cat->name.'</a>';
        $sub_cats=get_terms(TH_TAX_TYPE, array('parent'=>$main_cat->term_id ));
        if($sub_cats ){
            if($main_cat->slug==$term || $main_cat->term_id ==$get_current_term_info->parent || is_decendent($get_current_term_info->term_id,$main_cat->term_id)){
            $out_put.='<ul>';
            foreach ($sub_cats as $sub_cat) {
                $out_put.='<li><a href="'.get_term_link($sub_cat).'">'.$sub_cat->name.'</a> 
                    <small>('.$sub_cat->count.')</small>';
                    $sub_sub_cats=  get_terms(TH_TAX_TYPE, array('parent'=>$sub_cat->term_id));
                     if($sub_cat->slug==$term || $sub_cat->term_id ==$get_current_term_info->parent){
                    if($sub_sub_cats){
                        $out_put.='<ul>';
                        foreach ($sub_sub_cats as $sub_sub_cat) {
                          $out_put.='<li>
                              <a href="'.get_term_link($sub_sub_cat).'">'.$sub_sub_cat->name.'</a>
                                  <small>('.$sub_sub_cat->count.')</small></li>';
                        }
                        $out_put.='</ul>';
                    }}
                $out_put.='</li>';
            }
            $out_put.='</ul>';
            }
        }
         $out_put.='</li>';
    }
}
$out_put.='</ul>';
return $out_put;
}

CSS for Category Tree

ul.category-tree {
margin: 0 10px;
}
 ul.category-tree li {
margin-bottom: 0px;
padding-left: 0px;
padding: 5px 0px;
}
ul.category-tree li a {
font-size: 13px;
color: #8a6741;
}
ul.category-tree li ul {
border-left: 1px solid #999;
list-style: none;
margin-bottom: 0px;
margin-left: 0px;
margin-top: 7px;
}
ul.category-tree li ul li {
line-height: 15px;
padding-left: 17px;
position: relative;
}

ul.category-tree li ul li:before {
  background-color: #999;
  content: "";
  height: 1px;
  left: 0;
  position: absolute;
  top: 12px;
  width: 11px;
}

ul.category-tree li ul li a {
color: #555;
font-size: 13px;
}
ul.category-tree li ul li ul li a {
color: #555;
}
.category-tree.list {
overflow: hidden;
 }
.category-tree.list li {
float: left;
width: 100%;
margin: 0px 15px;
}
.category-tree.list li li {
float: none;
width: auto;
margin: inherit;
 }
.category-tree small {
font-size: 11px;
float: right;
}

Now you can echo category_tree() where you like or modify if you like.

I have done this way.

1
  • This looks promising but it's just throwing an error for me. =/
    – Lynne
    Jun 13, 2013 at 17:09
1

This is how I have dealt with this.

As I've said in my initial question, I'm not a great PHP programmer, but I think this is a pretty solid way to handle a taxonomy drill-down.

It uses the current URL that the user is on to extract variables for platforms and genres. (Ie: it puts the slug of each into a vairable)

I put the code for that in my functions.php file:

// I assign the variables a NULL value before the multiexplode function
// so that I can use them for if statements later.
$platforms = NULL;
$genre = NULL;

function multiexplode ($delimiters, $string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return $launch;

    }   

$requestURI = multiexplode(array("/", "?", "&"), $_SERVER['REQUEST_URI']); //cuts the array into sections for genre= and platform=
$requestURI = array_splice($requestURI, -2, 2); //drops blanks for "/" and "?"

foreach ($requestURI as $value) {
    $value = parse_str($value);
    }
unset($value);

And here is my archive.php file which handles the output for genre & genre children. The platform list is in a different file.

<?php get_header(); ?>
<div class="center">
<div id="padding">

<?php
if ($genre != NULL && $platforms !=NULL) { // If ganre and platforms both have a value (meaning they were selected)

$args = array( 'taxonomy' => 'genre', 'parent' => 0 );
$terms = get_terms('genre', $args);

    foreach ($terms as $term) {

        $term_list .= '<li><a href="?platforms='.$platforms.'&genre='.$term->slug.'">'.$term->name.'</a></li>';
    }

    echo '<ul>';
    echo $term_list;
    echo '</ul>';

// then we display children of selected genre term
$getGenre = get_term_by('slug', $genre, 'genre'); // Get the term using the genre slug
$current_genre_id = $getGenre->term_id; // get the term ID 
$current_genre_name = $getGenre->name; // get the term name

$term_id = $current_genre_id; // what tax parent to query children from. In this case, whatever genre they've selected.
$taxonomy_name = 'genre'; // name of taxonomy 
$termchildren = get_term_children( $term_id, $taxonomy_name ); // get children



echo '<h1>Sub-Categories in Category: <span style="color: green">' . $current_genre_name . '</span></h1>'; 


echo '<ul>';
foreach ( $termchildren as $child ) {

    $term = get_term_by( 'id', $child, $taxonomy_name );

    echo '<li><a href="?platforms='.$platforms.'&genre='.$genre.'+'.$term->slug.'">' . $term->name . '</a></li>';
}
echo '</ul>';

?>


    <div id="postList">
    <?php if ( have_posts() ) { while ( have_posts() ) : the_post();?>
        <h2>Latest Entries</h2>
    <ul><a title="<?php the_title();?>" href="<?php the_permalink();?>"><li>
        <span class="image"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></span>
        <span class="content">
        <h3><?php the_title();?></h3>
        <?php the_excerpt(); ?>
        </span>

    </ul></li></a>
    <?php endwhile; } else { echo 'No posts to display.'; }?>
    </div>


<?php 
} else {
// They have only selected a platform
$args = array( 'taxonomy' => 'genre', 'parent' => 0 );
$terms = get_terms('genre', $args);

    foreach ($terms as $term) {

        $term_list .= '<li><a href="?platforms='.$platforms.'&genre='.$term->slug.'">'.$term->name.'</a></li>';
    }

    echo '<ul>';
    echo $term_list;
    echo '</ul>';

//wp_nav_menu( array('menu' => 'genres', 'container' => false, 'menu_class' => 'genres', 'depth' => 0));
?>

    <div id="postList">
    <h2>Latest Entries</h2>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <ul><a title="<?php the_title();?>" href="<?php the_permalink();?>"><li>
        <span class="image"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></span>
        <span class="content">
        <h3><?php the_title();?></h3>
        <?php the_excerpt(); ?>
        </span>
    </ul></li></a>
    <?php endwhile; ?>
    </div>

    </div>
</div>
<?php } ?>
<?php get_footer(); ?>

Now I just need to add check for the + symbol because it's not handling those properly once you drill all the way down into children, but that's not a big deal.

I'm not sure what to do about the bounty since I've essentially answered my own question. I'll leave it open until it expires I suppose, and if someone can give me a better answer that does everything I need it to do in a cleaner package, I'll reward it to them.

0

I think this can be achieve with wordpress taxonomy template and WP_Query function. you can do like that:

1.create a page template which list all terms in platforms taxonomy

2.create taxonomy-platforms.php.In this taxonomy template list all genre taxonomy terms which are parent terms ( $term->parent==0 ).

3.create taxonomy-genre.php.In this taxonomy template test term is parent term or sub-term

<?php
        $term_id = $wp_query->queried_object->cat_ID;
        $term=get_term($term_id,'genre');
        if(!$term->category_parent){
            get_template_part('content','parent-taxonomy');
        }else{
            get_template_part('content','sub-taxonomy');
        }
?> 

if term is parent then content-parent-taxonomy.php is call, in which list all sub term of that taxonomy.while if it is sub then content-sub-taxonomy.php in which list all posts ( use global varible to pass, platforms taxonomy term you are searching/visiting and use it in loop for query).

4
  • I am trying this out now and will let you know how it goes! Thanks!
    – Lynne
    Jun 13, 2013 at 16:48
  • This is essentially what I'm talking about, except that with taxonomy term lists, they reset the query every time. I need them to append to the URL as the user drills down. Maybe I'm doing something wrong?
    – Lynne
    Jun 13, 2013 at 17:07
  • Okay.. I think I have achieved my desired results using your suggestions and this code for the term lists: $term_list .= '<a href="' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] . '&genre=' . $term->slug . '">' . $term->name . '</a>';
    – Lynne
    Jun 13, 2013 at 19:01
  • glad to help :) Jun 14, 2013 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.