WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display Linkable Posts for each Custom Taxonomy Term (12 posts)

  1. whipd09
    Member
    Posted 4 years ago #

    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?

  2. vtxyzzy
    Member
    Posted 4 years ago #

    Does using $myquery = new WP_Query() conflict with your main loop? Did you try using wp_reset_query() after your sidebar loop?

  3. whipd09
    Member
    Posted 4 years ago #

    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>";
    			}
    			}
    		?>
  4. vtxyzzy
    Member
    Posted 4 years ago #

    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>";
      }
    }
    ?>
  5. whipd09
    Member
    Posted 4 years ago #

    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.

  6. wprelief
    Member
    Posted 4 years ago #

    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!

  7. vtxyzzy
    Member
    Posted 4 years ago #

    @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.

  8. whipd09
    Member
    Posted 4 years ago #

    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).

  9. vtxyzzy
    Member
    Posted 4 years ago #

    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

  10. whipd09
    Member
    Posted 4 years ago #

  11. vtxyzzy
    Member
    Posted 4 years ago #

    Please mark this topic 'Resolved'.

  12. jloft
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic