Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create a menu that replicates my custom taxonomy hierarchy WordPress

  • SOLVED

I have a custom post type (“products”) with an associated custom taxonomy (“product_type”). So when I create a product, I choose in which category it goes.

The taxonomy is hierarchical and it goes like this:


Category 1
Sub-Category 1.1
Sub-Category 1.2
Sub-Category 1.3
Category 2
Category 3
Sub-Category 3.1
Sub-Category 3.2
etc, etc


What I want (and don’t know how to do it) is to create a menu that dynamically replicate my taxonomies, with the products associated to them.

The rendered html should be something like this:


<ul>
<!--first level-->
<li>
<a href="#">Category 1</a>
<ul>
<!--second level-->
<li>
<a href="#">Sub-category 1.1</a>
<!--third level-->
<ul>
<li><a href="#">product</a></li>
<li><a href="#">product</a></li>
</ul>
</li>


<!--second level-->
<li>
<a href="#">Sub-category 1.2</a>
<!--third level-->
<ul>
<li><a href="#">Product</a></li>
<li><a href="#">Product</a></li>
</ul>
</li>
</ul>
</li>

<!--first level-->
<li class="first-level">
<a href="#">Category 2</a>
<ul>
<!--second level-->
<li><a href="#">Product</a></li>

<!--second level-->
<li><a href="#">Product</a></li>

<!--second level-->
<li><a href="#">Product</a></li>
</ul>
</li>
</ul>


I know html, but I'm not too savvy in php.
Can anybody help?

Thanks!

Answers (2)

2013-06-26

Hariprasad Vijayan answers:

Hello,


Try this code
<?php
$args = array('type'=> 'products','parent'=> 0,'orderby'=> 'id','order'=> 'ASC','hide_empty'=> 0,'taxonomy'=> 'product_type',);
$categories = get_categories( $args );
echo '<ul>';
foreach ( $categories as $category ) {
echo '<li>' . $category->name . '</li>';
$subargs = array('type'=> 'products','child_of'=> $category->term_id,'orderby'=> 'id','order'=> 'ASC','hide_empty'=> 0,'taxonomy'=> 'product_type',);
$subcategories = get_categories( $subargs );
if($subcategories)
{
echo '<ul>';
foreach ( $subcategories as $subcategory ) {
echo '<a href="' . get_category_link( $subcategory->term_id ) . '">' . $subcategory->name . '</a>';
query_posts(array( 'post_type' => 'products','showposts' => -1,'tax_query' => array(
array(
'taxonomy' => 'product_type',
'terms' => $subcategory->term_id,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
while ( have_posts() ) : the_post();
echo '<li>';
?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
echo '</li>';
endwhile;
wp_reset_query();
}
echo '</ul>';
}
}
echo '</ul>';
?>


Hariprasad Vijayan comments:

Please ask if you have any doubt.


gprego comments:

Hi Hariprasad, thanks for your reply.
Your code works perfect.
It only needs one minor change.
A couple of categories doesn't have subcategories. So right after the parent category, it should list the products.
You can see it in the attached image (in my third comment to Remy) in the item "bombas de doble diafragma".

I think the solution should be: after the "if($subcategories)", add an else, and there, list the products.
But I wasn't able to do it. :(

Thanks again


Hariprasad Vijayan comments:

Hello,
Try this code

<?php
$args = array('type'=> 'products','parent'=> 0,'orderby'=> 'id','order'=> 'ASC','hide_empty'=> 0,'taxonomy'=> 'product_type',);
$categories = get_categories( $args );
echo '<ul>';
foreach ( $categories as $category ) {
echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>';
query_posts(array( 'post_type' => 'products','showposts' => -1,'tax_query' => array(array('taxonomy' => 'product_type','terms' => $category->term_id,'field' => 'term_id',)),'orderby' => 'title','order' => 'ASC' ));
if ( have_posts() ) {
echo '<ul>';
while ( have_posts() ) : the_post();
echo '<li>';
?><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php
echo '</li>';
endwhile;
echo '</ul>';
}
wp_reset_query();
$subargs = array('type'=> 'products','child_of'=> $category->term_id,'orderby'=> 'id','order'=> 'ASC','hide_empty'=> 0,'taxonomy'=> 'product_type',);
$subcategories = get_categories( $subargs );
if($subcategories)
{
echo '<ul>';
foreach ( $subcategories as $subcategory ) {
echo '<a href="' . get_category_link( $subcategory->term_id ) . '">' . $subcategory->name . '</a>';
query_posts(array( 'post_type' => 'products','showposts' => -1,'tax_query' => array(
array(
'taxonomy' => 'product_type',
'terms' => $subcategory->term_id,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
while ( have_posts() ) : the_post();
echo '<li>';
?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><?php
echo '</li>';
endwhile;
wp_reset_query();
}
echo '</ul>';
}
}
echo '</ul>';
?>


Ask if you are facing folder.


Hariprasad Vijayan comments:

Sorry... ask if you are facing any problem...


gprego comments:

Thanks again.
Something is wrong. The first code worked better.
Look at my new attach image.
In the second code sent, all the products appear together (not inside a sub-category), and all the subcategories appear together too.