列出按嵌套自定义分类排序的自定义帖子类型

时间:2013-07-18 作者:Igrinsh

正如标题所说,我有自定义的帖子类型和该类型的自定义分类法
对于分类法,我有如下3个级别的术语:

第1学期第1a学期第1b学期第2学期第2a学期第2b学期第2b-1学期第2b-2学期第2b-2学期第2b-1学期第2b-2学期第2b-2学期第2学期第2b-2学期第2b-2学期第2b-1学期第2b-2学期的所有帖子。但如何使这些帖子成为对应于这些级别的嵌套列表呢?并显示每个的标题。因此,列表如下所示:

<ul>
    <li>Term 1:
        <ul>
            <li>Post 1</li>
            <li>Post 2</li>
        </ul>
    </li>
    <li>Term 2:
        <ul>
            <li>Post 3</li>
            <li>Term 3:
                <ul>
                    <li>Post 4</li>
                    <li>Post 5</li>
                </ul>
            </li>
            <li>Post 6</li>
        </ul>
    </li>
</ul>
我有点不知所措,因为查询已经完成,所有需要的帖子都已返回,所以我可能不应该再打电话了。

我是否可以使用查询中的信息格式化此列表,或者我是否需要获取所有分类术语,然后对每个查找子项和相应的帖子进行格式化<当然,我不希望每个级别都有重复的帖子。

1 个回复
SO网友:Igrinsh

可能不是最好的解决方案,但我设法得到了我需要的东西,而且效果很好。我也可以添加到输出html ul和li标签,以便按照我在开始时的要求创建列表,但这对我来说是可以的,并且这种设计不需要更多的样式。

            $queried_taxonomy_name = $wp_query->queried_object->taxonomy;
            $queried_term_id = $wp_query->queried_object_id;
            $queried_term = get_term_by(\' id\', $queried_term_id, $queried_taxonomy_name );

            function show_term_posts( $queried_posts, $term, $taxonomy ){
                $term_children = get_term_children( $term->term_id, $taxonomy );
                global $queried_term_id;
                if ( $term->term_id != $queried_term_id ){
                    echo \'<h2>\' . $term->name . \'</h2>\';
                    echo wpautop( $term->description );
                }

                $queried_post_ids = array();
                foreach ($queried_posts as $post) {
                    $queried_post_ids[] = $post->ID;
                }
                unset($post);

                $childposts = array();
                $child_post_ids = array();
                $posts_to_return_ids = array();

                if( $term_children ){
                    $childposts = array();
                    $child_post_ids = array();
                    $posts_to_return_ids = array();
                    foreach ($term_children as $queried_term_child) {
                        $term_child = get_term_by(\'id\', $queried_term_child, $taxonomy );
                        if( $term_child->count > 0 ){
                            $args = array(
                                \'post_type\' => \'bk_menu\',
                                \'nopaging\'  => true,
                                \'tax_query\' => array(
                                    array(
                                        \'taxonomy\' => $taxonomy,
                                        \'field\' => \'id\',
                                        \'include_children\' => true,
                                        \'terms\' => array($queried_term_child)
                                    )
                                )
                            );
                            $childposts = array_merge( $childposts, get_posts( $args ) );
                        }
                    }
                    unset($queried_term_child);

                    foreach ($childposts as $child_post) {
                        $child_post_ids[] = $child_post->ID;
                    }
                    unset($child_post);

                    $child_post_ids = array_unique( $child_post_ids );
                    $posts_to_return_ids = array_diff( $queried_post_ids, $child_post_ids );

                } else {
                    $posts_to_return_ids = $queried_post_ids;
                }

                if( $posts_to_return_ids ){
                    $posts_to_return = get_posts( array(
                        \'post_type\' => \'bk_menu\',
                        \'nopaging\' => true,
                        \'post__in\' => $posts_to_return_ids
                    ) );
                }

                if($posts_to_return){
                    global $post;
                    foreach ( $posts_to_return as $post ) {
                        setup_postdata($post);
                        ?><div class="post"><? the_title(); the_content(); ?></div><?
                    }
                    unset($post);
                    wp_reset_postdata();
                }
                if($term_children){
                    foreach ($term_children as $term_child_id) {
                        $term_child = get_term_by(\'id\', $term_child_id, $taxonomy );
                        if( $term_child->count > 0 && $term_child->parent == $term->term_id ){
                            $args = array(
                                \'post_type\' => \'bk_menu\',
                                \'nopaging\'  => true,
                                \'tax_query\' => array(
                                    array(
                                        \'taxonomy\' => $taxonomy,
                                        \'field\' => \'id\',
                                        \'include_children\' => true,
                                        \'terms\' => array($term_child_id)
                                    )
                                )
                            );
                            $posts_to_return = get_posts( $args );
                            show_term_posts( $posts_to_return, $term_child, $taxonomy );
                        }
                    }
                }
            }

            show_term_posts( $posts, $queried_term, $queried_taxonomy_name );

结束

相关推荐

Wp_list_ages中的非页面链接

我想要一个这样的导航。家庭-人员-关于-服务-联系主页、关于、服务和联系人都是页面。人们没有页面,我想用这个链接打开第二个导航。是否可以在wp\\U list\\U页面中添加不链接到页面的链接。是否也可以向此链接添加没有页面的id。我希望代码看起来像这样。 <ul> <li><a href=\"Wordpress link\">Home</a></li> <li id=\"people\">