【问题标题】:Create a menu that replicates my custom taxonomy hierarchy in wordpress创建一个菜单,在 wordpress 中复制我的自定义分类层次结构
【发布时间】:2013-06-24 02:04:33
【问题描述】:

我有一个自定义帖子类型(“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不太精通。

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

谢谢!

【问题讨论】:

  • 嗨,我实际上在几周前为客户构建了一个具有类似结构的自定义导航。我想我可以提供帮助,但您介意分享更多有关信息如何存储在数据库中的信息吗?自定义帖子类型很简单,但是您如何标记分类法。是那些 page_meta 信息,还是别的什么?您可以分享的有关其工作原理的越多越好(即代码示例会很棒)。
  • 嗨,克里斯,感谢您的关注,但我找到了解决方案。你可以在我的回答中查看。再次感谢您的关注。
  • 很好......看起来或多或少像我的想法。很高兴你解决了!

标签: wordpress wordpress-theming custom-taxonomy


【解决方案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>';

?>

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 2015-11-17
    • 2017-08-23
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2018-06-05
    相关资源
    最近更新 更多