Ask your WordPress questions! Pay money and get answers fast! (more info)

Taxonomy template sub-term title & posts WordPress

  • SOLVED

I have a hierarchal taxonomy named 'types'. Types has 3 levels of terms: a business, its manufacturers, and their categories.

taxonomy-types.php is mainly being used for the child term (but not the grandchild). The child terms do not have any posts assigned to them, but the grandchild terms do. In its current form, when displaying a child term, the template is querying posts from the grandchild terms, which is fine.

What I'd like to be able to do is split the results up by the title of the grandchild terms, so when viewing a child term (manufacturer) the results would look like:

Category 1
- Product 1
- Product 2
- Product 3

Category 2
- Product 1
- Product 2
- Product 3

Here's the current template code:


<?php get_header(); ?>

<div id="content">
<div class="holder">

<div id="left" class="products">
<?php query_posts($query_string."&posts_per_page=-1"); ?>
<?php if ( have_posts() ) : $counter = 1; while ( have_posts() ) : the_post(); ?>
<?php if($counter == 4) { $counter = 1; } ?>
<div class="product prod<?= $counter; ?>">
<?php the_post_thumbnail('product'); ?>
<h2><?php the_title(); ?></h2>
<div class="points">
<?php the_excerpt(); ?>
</div>
<div class="buttons">
<a href="#"><img src="<?php bloginfo('template_directory'); ?>/images/button_quote.png" /></a>
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/button_more.png" /></a>
<?php
$pdf = get_post_meta($post->ID, 'eq_product_pdf', true);
if($pdf != '') {
?>
<a class="pdf" href="<?= $pdf; ?>" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/button_pdf.png" /></a>
<?php
}
?>
<div class="cboth"></div>
</div>
</div>
<?php $counter++; endwhile; endif; ?>
</div><!-- left -->

<?php get_sidebar('products'); ?>

<div class="cboth"></div>

</div><!-- holder -->
</div><!-- content -->

<?php get_footer(); ?>


Any help is appreciated!

Answers (1)

2012-11-14

Arnav Joy answers:

please try this and let me know if it's output is correct as what you want

here you have to check to things :-

1. $post_type = 'YOUR_POST_TYPE';

change 'YOUR_POST_TYPE' to post type you have

2. $term_id = $queried_object->term_id;

see what is the result of "current_term_id==" and if it is not returning any thing then try to place current term id manually to see if code is producing correct output .



<code><div id="content-main">

<?php

// List posts by the terms for a custom taxonomy of any post type

$post_type = 'YOUR_POST_TYPE';
$tax = 'types';

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
echo 'current_term_id=='.$term_id;
$tax_terms = get_terms( $tax , array( 'child_of' => $term_id , 'hide_empty' => 0 ) );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);

$my_query = null;
$my_query = new WP_Query($args);

if( $my_query->have_posts() ) : ?>

<h2 class="breadcrumb">All <?php echo $tax; ?> Posts For <?php echo $tax_term->name; ?></h2>
<ul class="taxlist">
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

<li id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>

<?php endwhile; // end of loop ?>

</ul>

<?php else : ?>
<?php endif; // if have_posts()
wp_reset_query();

} // end foreach #tax_terms
}
?>
</div>
<code>