ORIGINAL POST:
So I'm having an issue displaying the Articles that belong Subcats of a custom taxonomy.
What I'm currently forced to do with the loop below is have multiple pages: 1 for the Subcategories and 1 for the Articles that belong to that subcat.
What I want, rather, is a page that does this:
Main Category
Description of category
Subcategory 1
- Article
- Article
Subcategory 2
- Article
- Article
Here is my code:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id=="") && (sizeof($children)>0)) { // no parent, has child - core main categories
$args = array(
'child_of' => $term->term_id,
'taxonomy' => $term->taxonomy,
'hide_empty' => 0,
'hierarchical' => true,
'orderby'=>'menu_order',
'depth' => 1,
'title_li' => ''
);
echo "<h4>Subcategories</h4>";
wp_list_categories( $args );
//if there are articles in the main category as well
//echo "i have NO parent, but have children";
}elseif(($parent->term_id!="" && sizeof($children)>0)) { // has parent and child - mid level cats, we dont have these yet
//echo "i have a parent, and children";
}elseif(($parent->term_id=="") && (sizeof($children)==0)){ ?>
<h5>Articles</h5> <?
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h5 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5><?
endwhile;
endif;
//echo "i have NO parent and NO children";
}elseif(($parent->term_id!="") && (sizeof($children)==0)) { // has parent, no child - last branch on tree
?><h3>Articles</h3> <?
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h5 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5> <?
endwhile;
endif;
//echo "i have a parent, but NO children";*/
}
ORIGINAL POST:
So I'm having an issue displaying the Articles that belong Subcats of a custom taxonomy.
What I'm currently forced to do with the loop below is have multiple pages: 1 for the Subcategories and 1 for the Articles that belong to that subcat.
What I want, rather, is a page that does this:
Main Category
Description of category
Subcategory 1
- Article
- Article
Subcategory 2
- Article
- Article
Here is my code:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id=="") && (sizeof($children)>0)) { // no parent, has child - core main categories
$args = array(
'child_of' => $term->term_id,
'taxonomy' => $term->taxonomy,
'hide_empty' => 0,
'hierarchical' => true,
'orderby'=>'menu_order',
'depth' => 1,
'title_li' => ''
);
echo "<h4>Subcategories</h4>";
wp_list_categories( $args );
//if there are articles in the main category as well
//echo "i have NO parent, but have children";
}elseif(($parent->term_id!="" && sizeof($children)>0)) { // has parent and child - mid level cats, we dont have these yet
//echo "i have a parent, and children";
}elseif(($parent->term_id=="") && (sizeof($children)==0)){ ?>
<h5>Articles</h5> <?
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h5 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5><?
endwhile;
endif;
//echo "i have NO parent and NO children";
}elseif(($parent->term_id!="") && (sizeof($children)==0)) { // has parent, no child - last branch on tree
?><h3>Articles</h3> <?
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h5 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5> <?
endwhile;
endif;
//echo "i have a parent, but NO children";*/
}
Share
Improve this question
edited Feb 2, 2017 at 6:24
PrintF
asked Feb 2, 2017 at 2:30
PrintFPrintF
261 silver badge7 bronze badges
0
1 Answer
Reset to default 0UPDATE:
I have found a solution, as per several threads on here and elsewhere. Please use this if this is useful to you.
It is the code of the taxonomy.php file in my Genesis theme.
It does the following:
- Displays all the Child category names and the articles related to those categories
EXAMPLE:
(breadcrumb) Health Topic A Page
Main Subcategory 1
- Article 1
- Article 2
Main Subcategory 2
- Article 1
- Article 2
- It displays JUST the articles belong to "Main Subcategory X"
EXAMPLE:
(breadcrumb) Health Topic A Page > Main Subcategory 1
- Article 1
- Article 2
function child_do_custom_loop() {
//first get the current term
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id=="") && (sizeof($children)>0)) { // no parent, has child - core main categories
$args = array(
'child_of' => $term->term_id,
'taxonomy' => $term->taxonomy,
'hide_empty' => 0,
'hierarchical' => true,
'orderby'=>'menu_order',
'depth' => 1,
'title_li' => ''
);
$category = get_categories($args);
foreach ($category as $subcat) {
wp_reset_query();
echo "<h3>" . $subcat->name . "</h3>"; //Get Parent Category Name
$newargs = array(
'post_type' => 'health-topics',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => $subcat->slug,
),
),
);
$posts = new WP_Query($newargs);
while($posts->have_posts()) : $posts->the_post(); ?>
<h5 id="post-<?php the_ID(); ?>" style="margin-left:20px;">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5><?
endwhile;
} // end foreach
//echo "i have NO parent, but have children";
}elseif(($parent->term_id!="" && sizeof($children)>0)) { // has parent and child - mid level cats, we dont have these yet
//echo "i have a parent, and children";
}elseif(($parent->term_id=="") && (sizeof($children)==0)){ ?>
<h5>Articles</h5> <?
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h5 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5><?
endwhile;
endif;
//echo "i have NO parent and NO children";
}elseif(($parent->term_id!="") && (sizeof($children)==0)) { // has parent, no child - last branch on tree
?><h3>Articles</h3> <?
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h5 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h5> <?
endwhile;
endif;
//echo "i have a parent, but NO children";*/
}
}
/* END */