Support » Fixing WordPress » Display Linkable Posts for each Custom Taxonomy Term

  • Resolved whipd09

    (@whipd09)


    Hi guys, I’m trying to make a sidebar nav that is based on a custom taxonomy and it’s (custom post type) posts.

    See an screenshot here of it working in static form:
    http://skitch.com/whipd/dmcjw/portfolio-navigation.psd-100-group-2-rgb-8

    Obviously wp_list_categories could work to display each term but I need the term’s posts as well.

    I found a wp_query way of doing it but it kills the main loop of the page. I need it just to work in the sidebar without it conflicting with the existing page loop. Any ideas?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Does using $myquery = new WP_Query() conflict with your main loop? Did you try using wp_reset_query() after your sidebar loop?

    Thread Starter whipd09

    (@whipd09)

    Nope, doesn’t work. But the code I’m using isn’t actually a loop (I think).

    This is it:

    <?php
    			$terms = get_terms('typeofproject');
    
    			foreach ($terms as $term) {
    			$wpq = array ('taxonomy'=>'typeofproject','term'=>$term->slug);
    			$query = new WP_Query ($wpq);
    			$article_count = $query->post_count;
    			echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
    			echo $term->name;
    			echo "</h3>";
    			if ($article_count) {
    			echo "<ul>";
    			$posts = $query->posts;
    			foreach ($posts as $post) {
    			echo "<li><a href=\"".get_permalink()."\">".$post->post_title."</a></li>";
    			}
    			echo "</ul>";
    			}
    			}
    		?>

    Give this a try:

    <?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 "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
      echo $term->name;
      echo "</h3>";
      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>";
      }
    }
    ?>
    Thread Starter whipd09

    (@whipd09)

    Thank-you vtxyzzy it worked! So I was missing an endwhile; ?

    Is it also possible to have in there; only lists posts of the term that’s being viewed (is_taxonomy()?). So the nav only expands for the section you’re currently in.

    vtxyzzy is using the WordPress loop to your benefit here, because when you do the query using WP_Query, you get an object back. The WP Loop (using ->have_posts()) will pre-set teh $post object from the data returned with WP_Query. So, then when you create yours you can access the functions and object values that are overloaded by the loop.

    This is a very technical (confusing) way to say that vtxyzzy is using the wordpress loop to give you what you want, where you used foreach which does not set everything up the same way.

    Awesome that you finally got an answer!

    @whipd09, it wasn’t just the endwhile – as wprelief said, the foreach did not set up the $post variable the same way that have_posts() does.

    I don’t have a ready answer for your second question.

    Thread Starter whipd09

    (@whipd09)

    Thanks guys! I really hope I can comprehend this one day.

    In terms of that last request (if you dare help me more) I just need an if (it’s this taxonomy term page) show the sub-posts. if not hide them (display: none).

    The easy part is the if statement. The hard part is actually being able to tell which term page you are currently seeing.

    Do you display the term somewhere on the page? If so, please post the statement that displays the term.

    Since the format of this thread is so messed up, please start a new thread and post a link to it here.

    Thanks

    Thread Starter whipd09

    (@whipd09)

    Please mark this topic ‘Resolved’.

    vtxyzzy,

    Any thoughts on how one might modify your code to generate “related posts” in the sidebar of a single.php template? Is there a way to dynamically populate the first line of your code with the taxonomy associated with the main post being displayed by the single template?

    $terms = get_terms('taxonomy-of-main-post');

    I know how to do this with the get_the_category(); function, but I can’t figure out how to do it when using custom posts with a custom taxonomy. I’ve posted about this in my forum entry here:

    http://wordpress.org/support/topic/custom-taxonomy-related-posts-query

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Display Linkable Posts for each Custom Taxonomy Term’ is closed to new replies.