Question

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 point me in the right direction to accomplish that?

Thanks!

Was it helpful?

Solution

After searching for a while I decided that it would be best to pay somebody to figure this out, and here I found the solution "http://www.wpquestions.com/question/show/id/8543".

Thanks to "Hariprasad" for the solution.

<?php

$args = array('type'=> 'products','parent'=> 0,'child_of'=>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','parent'=> 0,'child_of'=>0,'showposts' => -1,'tax_query' => array(

            array('include_children'=>false,

            '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>';

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top