WordPress.org

Support

Support » How-To and Troubleshooting » Taxonomy Navigation – Accordion Functionality

Taxonomy Navigation – Accordion Functionality

  • Thanks to vtxyzzy and wprelief I’ve managed to get a sidebar navigation going based off of Taxonomy Terms and their posts as sub lists. The code is as follows:

    <ul id="port_nav">
    		<?php
    		$terms = get_terms('typeofproject');
    
    		foreach ($terms as $term) {
    		  $wpq = array ('taxonomy'=>'typeofproject','term'=>$term->slug);
    		  $myquery = new WP_Query ($wpq);
    		  $article_count = $myquery->post_count;
    		  echo "<li class=\"term-heading\" id=\"".$term->slug."\">";
    		  echo "<a href=\"#".$term->slug."\">".$term->name."</a>";
    		  echo "</li>";
    		  echo "<li style='padding: 0;' class=''>";
    		  if ($article_count) {
    			echo "<ul>";
    			while ($myquery->have_posts()) : $myquery->the_post();
    			  echo "<li>> <a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
    			endwhile;
    			echo "</ul>";
    		  }
    		  echo "</li>";
    		}
    		?>
            </ul>

    The only part I have left to achieve is hiding the posts of terms that don’t relate to the current page. You can see an end (static) example of how it should look here:
    http://skitch.com/whipd/dmcjw/portfolio-navigation.psd-100-group-2-rgb-8

    Doing an If Statement that using CSS to hide the irrelevant posts seems easy enough but as mentioned in the previous post the trouble is determining which term page you are currently on.

    To add to the troubles there’s a core bug in the system that prevents you using is_tax(‘something’). Please see the ticket here. I’m not sure what that means but a friend passed me on to it. It may be fixed by now?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thanks for starting a new thread – trying to read the other one was giving me a headache! Please mark the other one ‘Resolved’.

    I don’t think is_tax() is what should be used. is_tax(‘farful’) asks the question ‘Is this the farful archive page?’. The question we want is ‘What archive page is this?’. is_tax() does not tell us which archive page we are on.

    Is there anything in your page that shows the name of the taxonomy on the screen? Please post a link to your site and tell us where the taxonomy name is on the screen.

    I think, but haven’t tested, that this will work:

    <ul id="port_nav">
    <?php
    $current_tax = single_cat_title("", false);
    $terms = get_terms('typeofproject');
    
    foreach ($terms as $term) {
       if ($term->name == $current_tax) continue;
       $wpq = array ('taxonomy'=>'typeofproject','term'=>$term->slug);
       $myquery = new WP_Query ($wpq);
       $article_count = $myquery->post_count;
       echo "<li class=\"term-heading\" id=\"".$term->slug."\">";
       echo "<a href=\"#".$term->slug."\">".$term->name."</a>";
       echo "</li>";
       echo "<li style='padding: 0;' class=''>";
       if ($article_count) {
          echo "<ul>";
          while ($myquery->have_posts()) : $myquery->the_post();
             echo "<li>> <a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
          endwhile;
          echo "</ul>";
       }
       echo "</li>";
    }
    ?>
    </ul>

    OK – I had a chance to test that and it didn’t work, but this should.

    <ul id="port_nav">
    <?php
    $terms = get_terms('typeofproject');
    
    foreach ($terms as $term) {
       if (!is_tax($term->slug)) continue;
       $wpq = array ('taxonomy'=>'typeofproject','term'=>$term->slug);
       $myquery = new WP_Query ($wpq);
       $article_count = $myquery->post_count;
       echo "<li class=\"term-heading\" id=\"".$term->slug."\">";
       echo "<a href=\"#".$term->slug."\">".$term->name."</a>";
       echo "</li>";
       echo "<li style='padding: 0;' class=''>";
       if ($article_count) {
          echo "<ul>";
          while ($myquery->have_posts()) : $myquery->the_post();
             echo "<li>> <a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
          endwhile;
          echo "</ul>";
       }
       echo "</li>";
    }
    ?>
    </ul>

    Hi vtxyzzy,

    Thanks for the help but unfortunately that kills the whole menu entirely. Syntax error floating in there possibly? (Though I couldn’t spot any myself)

    I think probably that is_tax isn’t working the way I thought it would. Try changing this:

    if (!is_tax($term->slug)) continue;

    to this:

    if (false && !is_tax($term->slug)) continue;

    If that gets the menu back, then is_tax is never finding a match. Are you looking at a taxonomy page?

    On a non-taxonomy page, what do you want to show? Nothing, or the whole menu. If the whole menu, try this:

    if (is_tax() && !is_tax($term->slug)) continue;

    Hi vtxyzzy,

    that first solution just listed them all out at once (every type of project was expanded with their posts under each) and then the second one did the same thing except went blank on the main page.

    The main portfolio page is a WordPress page. Eg. http://www.site.com/ourportfolio (Nothing expanded)

    The taxonomy pages are archive pages. Eg. http://www.site.com/typeofproject/residential (Relevant TypeofProject expanded with posts)

    It also exists in the sidebar of each single portfolio page – which is a custom post type single page eg. http://www.site.com/portfolio/middleton-residence/ (Relevant TypeofProject expanded with posts)

    vtxyzzy if you wish to see a near working version of the site please email me at dwhipps at gmail dot com

    The first ‘solution’ wasn’t a solution – it was a test. Since it showed the entire menu, that means is_tax($term->slug) is always returning FALSE. It never recognizes the taxonomy on the current page.

    I don’t know what the answer is, but I do know that you need some other means of detecting which taxonomy you are currently showing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Taxonomy Navigation – Accordion Functionality’ is closed to new replies.
Skip to toolbar