I am trying to query a list with all Categories, Subcategories and Posts of a custom post type.
e.g.:
Maincat1
-Subcat1
-Post1
-Post2
-Subcat2
-Subcat3
Maincat2
-Subcat3
-Post3
Maincat3
-Post4
I'm getting mad trying to accomplish that. I tried out so many things that I found on the web but I can't figure out how to get it to work.
What I have so far (working), All Categories + Subcategories but i don't get it how to query the posts from the Categories / Subcategories.
<?php
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 1; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => 'kategorie',
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<?php
$parent_cat_arg = array('hide_empty' => false, 'parent' => 0 );
$parent_cat = get_terms('kategorie',$parent_cat_arg);//category name
foreach ($parent_cat as $catVal) {
echo '<h3>'.$catVal->name.'</h3>'; //Parent Category
$args = array('post_type' => 'Dokumente',
'tax_query' => array(
array(
'taxonomy' => 'kategorie',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<ul><li><a href="#" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><li></ul><?php
endwhile;
}
wp_reset_query();
$child_arg = array( 'hide_empty' => false, 'parent' => $catVal->term_id );
$child_cat = get_terms( 'kategorie', $child_arg );
echo '<ul>';
foreach( $child_cat as $child_term ) {
echo '<li>'.$child_term->name . '</li>'; //Child Category
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<ul><li><a href="#" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><li></ul><?php
endwhile;
}
wp_reset_query();
}
echo '</ul></li>';
}
?>
</div>
<?php
}
add_shortcode('kategorien', 'get_mylist' );
I also have the code to query the posts (at least i think it works) - but i don't know how to combine them.
$custom_terms = get_terms('kategorie');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args_slugs = array('post_type' => 'Dokumente',
'tax_query' => array(
array(
'taxonomy' => 'kategorie',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args_slugs);
if($loop->have_posts()) {
while($loop->have_posts()) : $loop->the_post();
echo '<ul><li><a href="'.get_permalink().'">'.get_the_title().'</a></li></ul><br>';
endwhile;
}
}
I really hope someone can help me... And yes I already used the search and found similar questions but none of the answers worked for me.
Best, NixXxon
$custom_term->slug
however there is no variable defiend with this name.