Does using $myquery = new WP_Query()
conflict with your main loop? Did you try using wp_reset_query() after your sidebar loop?
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>";
}
}
?>
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.
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
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