0

我有一个自定义帖子类型(“products”)和关联的自定义分类(“product_type”)。因此,当我创建产品时,我会选择它所属的类别。

分类是分层的,它是这样的:

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

我想要(但不知道该怎么做)是创建一个动态复制我的分类法的菜单,以及与之关联的产品。

呈现的 html 应该是这样的:

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

我知道 html,但我对 php 不太精通。

谁能指出我正确的方向来实现这一目标?

谢谢!

4

1 回答 1

1

搜索了一段时间后,我决定最好花钱请人来解决这个问题,在这里我找到了解决方案“ http://www.wpquestions.com/question/show/id/8543 ”。

感谢“Hariprasad”的解决方案。

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

?>
于 2013-06-27T17:56:50.307 回答