I have a CPT set-up as below -
Registered CPT - Name = 'govenors'
Registered Taxonomy Name = 'govenor-groups'
I have entered some 'Parent' terms as below -
Parent Terms - 'governing-body' , 'governing-committees' etc
When i view the taxonomy archive for the parent term 'governing-body' i want the display to be as shown below. Display the Parent term heading and then List all the child term headings with the child posts of each child term below. the posts are just <title> - custom fields.
<strong>Taxonomy Parent Term Archive Layout</strong>
Display - <strong>{parent-term}</strong> name as heading
Display - <strong>{child-term}</strong> name
- post<title> - (custom fields)
- post<title> - (custom fields)
- post<title> - (custom fields)
Display - <strong>{child-term}</strong> name
- post<title> -(custom fields)
- post<title> - (custom fields)
- post<title> - (custom fields)
etc , etc
I'd want this layout to be used for any parent terms under the Registered Taxonomy Name = 'govenor-groups'
I think the specific file would be named <strong>taxonomy-govenor-groups-governing-body.php</strong>
But i want to avoid having a separate template for each parent term i.e
'taxonomy-govenor-groups-governing-body.php'
'taxonomy-govenor-groups-governing-committees.php'
I would like to have any new parent terms that are added to display this way automatically.
is this possible?
I'm using the code below (found [[LINK href="http://wpquestions.com/question/showChrono/id/10819"]]here at wpquestions[[/LINK]]) on my CPT archive page and this shows the Taxonomies in the way i want , but shows ALL OF THEM , but for the specific term archive i don't want all of them , I only want the term parent name as a heading with the child terms headings and the posts below, can i edit this code to achive that ?
$post_type = 'govenors';
$tax = 'govenor-groups';
$tax_args = array(
'order' => 'ASC',
'parent' => 0
);
// get all the first level terms only
$tax_terms = get_terms( $tax, $tax_args );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) { // foreach first level term
// print the parent heading
?>
<h4 class="gov-parent-term"><?php echo $tax_term->name; ?></h4>
<?php
// get all its children
$child_terms = ""; // first ensure this var is empty
$child_terms = get_terms ( $tax, array('order' => 'ASC', 'parent' => $tax_term->term_id) );
// store an array of child terms slug
$child_terms_array = array();
foreach ($child_terms as $child_term){
$child_terms_array[] = $child_term->slug;
}
// if any, foreach child term, query the posts
if ( !empty($child_terms) ){
foreach ($child_terms as $child_term){
$child_args="";
$child_args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $child_term->slug,
'include_children' => false,
'operator' => 'IN'
)
),
'post_status' => 'publish',
'order' => 'ASC',
);
// query the posts
$child_query = null;
$child_query = new WP_Query($child_args);
if( $child_query->have_posts() ) : ?>
<h3><?php echo $child_term->name; ?></h3>
<ul>
<?php while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; // end of loop ?>
</ul>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #child_terms
}
} // end foreach #parent_term
}
dimadin answers:
This is not tested (quick solution) but it should be like this:
$post_type = 'govenors';
$tax = 'govenor-groups';
$term = get_queried_object();
// print the parent heading
?>
<h4 class="gov-parent-term"><?php echo $term->name; ?></h4>
<?php
// get all its children
$child_terms = ""; // first ensure this var is empty
$child_terms = get_term_children ( $term->term_id, $tax );
// if any, foreach child term, query the posts
if ( !empty($child_terms) ){
foreach ($child_terms as $child_term){
$child_args="";
$child_args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $child_term->slug,
'include_children' => false,
'operator' => 'IN'
)
),
'post_status' => 'publish',
'order' => 'ASC',
);
// query the posts
$child_query = null;
$child_query = new WP_Query($child_args);
if( $child_query->have_posts() ) : ?>
<h3><?php echo $child_term->name; ?></h3>
<ul>
<?php while ( $child_query->have_posts() ) : $child_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; // end of loop ?>
</ul>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #child_terms
}
If this works for you I can add a few improvements.
andytc comments:
Tried it , it only returns the parent term heading correctly -
<h4 class="gov-parent-term"><?php echo $term->name; ?></h4>
None of the child term headings or post under that show up .... just a blank page with the parent term heading as above.
If i change -
'include_children' => false,
to <strong>true</strong> - it shows all the parent terms and children , pretty much as before
dimadin comments:
Uh, I see where I made mistake, change line
$child_terms = get_term_children ( $term->term_id, $tax );
to
$child_terms = get_terms ( $tax, array('order' => 'ASC', 'include' => get_term_children ( $term->term_id, $tax )) );
andytc comments:
That's it ! :)
Top job , thank you !