Loops and Multiple Terms in menu
-
Hello, I have a question.
First off let me explain what im trying to do here.
In WordPress i created custom posts(portfolio), with their own taxonomy and terms. I created a menu that displays items in their main categories and in subcategories, however this only works when these items have only 1 term selected. As soon as they have more than 1 term selected they will be displayed only under the first category (term). The menu has to show the items under every category it is included in.
In the code below i use a WP_Query to call onto all posts, and some of these posts have multiple terms.
I think part of the problem is with this: if($term_list[0] == $term->name){
It compares only the first term and therefore it will only use the first term and not any other ones, I however do not know how to solve this.
Any and all help would be greatly appriciated.
You can visit the site here: http://portfolio.custompost.nl/portfolio
echo '<div id="javascript_wrapper">'; //Open Javascript wrapper echo '<div id="accordion">'; //Accordion wrapper for Categories //Loops all posts, this is to display them all in the menu. $menuLoop = new WP_Query ( array ( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'order'=> 'ASC', 'orderby'=> 'menu_order', ) ); foreach ($terms as $term) { //ID of the term to get the children from, and variables for get_term. $term_id = $term->term_id; $taxonomy_name = $taxonomy[0]; $termchildren = get_term_children( $term_id, $taxonomy_name ); //Check if it's an error before continuing $term_link = get_term_link( $term, $taxonomy); if( is_wp_error( $term_link ) ) continue; //If the parent term has children, place them in a submenu. if($termchildren){ echo '<h3><span class="accordionplus"></span><span class="accordionheaders">'.$term->name.'</span></h3>'; //Headers for Categories echo '<div>'; //Content wrapper for Categories echo '<ul>'; //List for Category //level 1 ul while ( $menuLoop->have_posts() ) : $menuLoop->the_post(); $term_list = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "names")); if($term_list[0] == $term->name){ echo '<li><a title="'.get_the_title().'" href="'.get_permalink().'">'.get_the_title().'</a></li>'; //List items for Category //level 1 li } endwhile; // end of the loop echo '<div class="subaccordion">'; //Accordion wrapper for Subcategories foreach ( $termchildren as $child ) { $thechildterm = get_term_by( 'id', $child, $taxonomy_name ); echo '<li>'; //List item for Category //level 1 li echo '<h3><span class="accordionplus"></span><span class="accordionheaders">'.$thechildterm->name.'</span></h3>'; //Headers for Subcategories echo '<div>'; //Content wrapper for Subcategories echo '<ul>'; //List for Subcategory //level 2 ul while ( $menuLoop->have_posts() ) : $menuLoop->the_post(); $term_list = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "names")); if($term_list[0] == $thechildterm->name){ echo '<li><a title="'.get_the_title().'" href="'.get_permalink().'">'.get_the_title().'</a></li>'; //List items for Subcategory //level 2 li } endwhile; // end of the loop echo '</ul>'; //Close List for Subcategory //level 2 ul echo '</div>'; //Close content wrapper for Subcategories echo '</li>'; //Close List item for Category //level 1 li } echo '</div>'; //Close Accordion wrapper for Subcategories echo '</ul>'; //Close List for Category //level 1 ul echo '</div>'; //Close content wrapper for Categories } else { echo '<h3><span class="accordionplus"></span><span class="accordionheaders">'.$term->name.'</span></h3>'; echo '<div>'; echo '<ul>'; while ( $menuLoop->have_posts() ) : $menuLoop->the_post(); $term_list = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "names")); if($term_list[0] == $term->name){ echo '<li><a title="'.get_the_title().'" href="'.get_permalink().'">'.get_the_title().'</a></li>'; } endwhile; // end of the loop echo '</ul>'; echo '</div>'; } } echo '</div>'; //Close Accordion wrapper for Categories echo '</div><!--javascriptwrapper-->'; //Close Javascript wrapper
The topic ‘Loops and Multiple Terms in menu’ is closed to new replies.