Estoy diseñando una biblioteca personal en wordpress con Custom Post Types y shordcodes de Wordpress, logro mostrar categorias y subcategorias y post, pero cuando intento colocar los post pertenecientes a las categorias superiores me doy con la sorpresa que también coloca los post de la subcategorias
Debería quedar así
Categorias
-Post Categoria
-Sub Categoria
--Post Subcategoria
Pero actualmente lo tengo asi
Categorias
-Post Categoria
-Post Subcategoria
-Sub Categoria
--Post Subcategoria
Tome el código de aqui y lo adapte a mis necesidades, adjunto código utilizado, me disculpo por el desorden
function mostrar_categoria_fuentes( $atts ){
$params = shortcode_atts( array(
'categoria' => 'Categoria',
), $atts );
$post_type = 'fuentes';
$taxonomy = 'fuentes_categorias';
// Fetch root categories
$root_cat_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => 0,
'post_status' => 'publish',
);
$content .= "<ul id='menu'>";
$root_categories = get_categories($root_cat_args);
foreach ($root_categories as $category) {
$content .= "<li class='primer-nivel-desplegable'><input type='checkbox' name='list' id=".$category->term_id."><label for=".$category->term_id.">".$category->name."</label>";
// Fetch sub categories
$sub_cat_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $category->term_id,
'post_status' => 'publish',
);
$sub_categories = get_categories($sub_cat_args);
$content .= "<ul class='interior'>";
foreach ($sub_categories as $category) {
$content .= "<li class='segundo-nivel-desplegable'><input type='checkbox' name='list' id=".$category->term_id."><label for=".$category->term_id.">".$category->name."</label>";
// Fetch sub categories
$sub_sub_cat_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $category->term_id,
'post_status' => 'publish',
);
$sub_sub_categories = get_categories($sub_sub_cat_args);
$content .= "<ul class='interior'>";
foreach ($sub_sub_categories as $category) {
$content .= "<li class='tercer-nivel-desplegable'><input type='checkbox' name='list' id=".$category->term_id."><label for=".$category->term_id.">".$category->name."</label>";
// Fetch sub categories
$sub_sub_cat_args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'parent' => $category->term_id,
'post_status' => 'publish',
);
$sub_sub_categories = get_categories($sub_sub_cat_args);
$content .= "<ul class='interior'>";
// Fetch root category posts
$root_posts_args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'include_children' => false,
'terms' => $category->term_id,
),
),
);
$root_posts = new WP_Query($root_posts_args);
foreach ($root_posts->posts as $post) {
// Print root category posts
$content .= "<li><a href='".$post->guid."'>".$post->post_title."</a></li>";
}
foreach ($sub_sub_categories as $category) {
$content .= "<li class='cuarto-nivel-desplegable'><input type='checkbox' name='list' id=".$category->term_id."><label for=".$category->term_id.">".$category->name."</label>";
// Print all posts of this sub category
$sub_posts_args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'include_children' => false,
'terms' => $category->term_id,
),
),
);
$root_posts = new WP_Query($sub_posts_args);
$content .= "<ul class='interior'>";
foreach ($root_posts->posts as $post) {
// Print root category posts
$content .= "<li class='listado-interior'><a href='".$post->guid."'>".$post->post_title."</a></li>";
}
$content .= "</ul>";
// Final Print all posts of this sub category
$content .= "</li>";
}
$content .= "</ul>";
$content .= "</li>";
}
$content .= "</ul>";
$content .= "</li>";
}
$content .= "</ul>";
$content .= "</li>";
}
$content .= "</ul>";
//Reset query
return $content;
}
add_shortcode('mostrar_cat_fuentes', 'mostrar_categoria_fuentes');