是否创建按术语顺序遍历子类别的自定义分类模板?

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

我们正在建立一个网站Biking Gear

我们正在使用Genesis主题框架,并开发一个自定义子主题。

它有一个定制的产品帖子类型,使用“定制帖子类型UI插件”制作。

它还具有针对产品类别的自定义分类法,我们为自定义帖子类型分类法归档创建了一个模板:taxonomy-products-category.php

模板需要能够循环浏览产品类别中的所有帖子,但要按照term\\u顺序进行排序。

例如/product-categories/cycling/ 需要按照我们设置的顺序循环“自行车”的子类别(我认为是term\\u顺序)

此外,如果是子类别,如左侧菜单中所示,模板只会循环浏览该类别,因此如果用户单击“骑自行车”,模板将显示骑自行车类别中的所有帖子,并按子类别(球衣、短裤、背心…)分组并输出每个上方的存档标题,

我不知道该如何清理,所以我试着拼凑了一些导致失败的片段,并向社区寻求建议。

1 个回复
SO网友:AhSo

这是我们到目前为止得出的结论,肯定还有改进的余地。


remove_action( \'genesis_loop\', \'genesis_do_loop\' );

add_action( \'genesis_loop\', \'ahso_custom_taxonomy_archive_loop\' );

function ahso_custom_taxonomy_archive_loop() {

    global $wp_query; // globalize it.

    // get category ID
    $cat_id = $wp_query->get_queried_object_id(); // get current category

    // get all child categories
    $cat_args = array(
        \'type\'                     => \'products\',
        \'child_of\'                 => $cat_id,
        //\'parent\'                   => \'\',
        \'orderby\'                  => \'term_group\',
        \'order\'                    => \'ASC\',
        \'hide_empty\'               => 0,
        \'hierarchical\'             => 1,
        \'exclude\'                  => \'\',
        \'include\'                  => \'\',
        \'number\'                   => \'\',
        \'taxonomy\'                 => \'product-categories\',
        \'pad_counts\'               => false
    );

    // get all children of the current category
    $categories = get_categories( $cat_args );

    // if the cat has kittens, loop thru them in term order
    if ( !empty($categories) ) {

        foreach ($categories as $category) {

            // first output the category archive headline
            // gets the term table w/ the meta info added by genesis that contains the archive headline and intro text
            $term = get_term_by( \'id\', $category->term_id, $category->taxonomy );

            echo \'\';
            echo \'\'.$term->meta["headline"].\'\';
            echo \'\';

            // then build a query to loop thru the posts in that cat

            // WP_Query arguments
            $args = array (
                \'post_type\'              => \'products\',
                \'order\'                  => \'ASC\',
                \'orderby\'                => \'menu_order\',
                \'tax_query\'                 => array(
                    array(
                        \'taxonomy\'          => \'product-categories\',
                        \'field\'             => \'id\',
                        \'terms\'             => $category->term_id,
                        \'include_children\'  => false, // no kittens
                    )
                )
            );

            // loop thru posts in category
            // output featured image

            // The Query
            $cat_query = new WP_Query( $args );

            // The Loop
            if ( $cat_query->have_posts() ) {
                while ( $cat_query->have_posts() ) {
                    $cat_query->the_post();

                    // do something
                    echo \'\';
                    echo \'\';
                    echo the_post_thumbnail(\'medium\');
                    echo \'\';
                    echo \'\'.get_the_title().\'\';
                    echo \'\';

                }
            } else {
                // no posts found
            }

            // Restore original Post Data
            wp_reset_postdata();

        }

    } // end category children loop

    // if the category has no children, just run a loop thru that category.
    // get_categories returns an empty array
    if ( empty($categories) ) {

        //echo \'this cat has no kittens\';

        // WP_Query arguments
        $args = array (
            \'post_type\'              => \'products\',
            \'order\'                  => \'ASC\',
            \'orderby\'                => \'menu_order\',
            \'tax_query\'                 => array(
                array(
                    \'taxonomy\'          => \'product-categories\',
                    \'field\'             => \'id\',
                    \'terms\'             => $cat_id, // get the initial $cat_id, from the query object
                    \'include_children\'  => false, // pls spay and neutr yrs pets
                )
            )
        );

        // The Query
        $cat_query = new WP_Query( $args );

        // The Loop
        if ( $cat_query->have_posts() ) {
            while ( $cat_query->have_posts() ) {
                $cat_query->the_post();

                // do something
                echo \'\';
                echo \'\';
                echo the_post_thumbnail(\'medium\');
                echo \'\';
                echo \'\'.get_the_title().\'\';
                echo \'\';

            }
        } else {
            // no posts found
        }

        // Restore original Post Data
        wp_reset_postdata();


    } // end no children category loop

}

如果你有任何想法,请告诉我们。欢迎反馈。

结束

相关推荐

Pagination and multiple loops

对,所以我现在有一个使用css网格将页面分成三部分的页面。第三列是侧栏,前两列各有一个要显示的帖子查询,并创建一个漂亮的帖子网格(虚拟内容):http://puu.sh/2Xh9o.jpg每个循环如下所示: <?php query_posts(\'showposts=5\'); ?> <?php $posts = get_posts(\'numberposts=5&offset=0\'); foreach ($posts as $post) : start_wp()