分类.php上的WordPress自定义帖子类别和子类别

时间:2017-01-10 作者:Alex P.

我已经定制了带有标准类别和子类别的“产品”。现在,我需要在当前类别包含的类别页面上显示产品列表。

如果类别没有子类别,我需要显示以下内容:

<ul class="cat-arc-links">
    <li><a href="#" class="product-link">Product 1</a></li>
    <li><a href="#" class="product-link">Product 2</a></li>
    <li><a href="#" class="product-link">Product 3</a></li>
......
</ul>
如果类别包含子类别,则显示:

<span class="subhead-title">Subcat 1</span>
<ul class="cat-arc-links">
    <li><a href="#" class="product-link">Product 1</a></li>
    <li><a href="#" class="product-link">Product 2</a></li>
    <li><a href="#" class="product-link">Product 3</a></li>
......
</ul>

<span class="subhead-title">Subcat 2</span>
<ul class="cat-arc-links">
    <li><a href="#" class="product-link">Product 1</a></li>
    <li><a href="#" class="product-link">Product 2</a></li>
    <li><a href="#" class="product-link">Product 3</a></li>
......
</ul>
我怎样才能categories.php 来完成我的案子?提前感谢!

4 个回复
最合适的回答,由SO网友:Alex P. 整理而成

谢谢大家!这是我的解决方案,效果很好

<?php 

$cat = get_query_var(\'cat\');

$categories = get_categories(\'parent=\'.$cat.\'\'); 

if(isset($categories) && !empty($categories)){ 

    foreach ($categories as $category) { ?>

        <span class="subhead-title"><?php echo $category->name; ?></span>

        <?php $prods = new WP_query(); $prods->query(\'post_type=products&cat=\' . $category->cat_ID . \'\'); ?>
            <?php if($prods->have_posts()) { ?> <ul class="cat-arc-links"> <?php while ($prods->have_posts()) { $prods->the_post(); ?>

                 <li><a href="<?php the_permalink(); ?>" class="product-link"><?php the_title(); ?></a></li>


             <?php } ?> </ul> <?php } ?>

<?php }

}
else 
{
    global $query_string; // basic query parameters
    query_posts( $query_string.\'&post_type=products\'); // basic query + self parameters 

    if( have_posts() ) { ?> <ul class="cat-arc-links"> <?php  while( have_posts() ){ the_post(); ?>

            <li><a href="<?php the_permalink(); ?>" class="product-link"><?php the_title(); ?></a></li>

<?php } /* end of while */  wp_reset_query(); ?>
</ul>
    <div class="navigation">
        <div class="next-posts"><?php next_posts_link(); ?></div>
        <div class="prev-posts"><?php previous_posts_link(); ?></div>
    </div>

<?php
    } 
    else 
        echo "<h2>No entries.</h2>";
}
?>

SO网友:Marc-Antoine Parent

一旦有了主类别(加载到categories.php模板的类别),就可以使用函数get_term_children() 为了得到它所有的孩子。

如果没有子级,它将返回false或空数组。在下面的示例中,我假设您的产品的分类法是nameproducts_categories.

<?php 
    $term = <youPrimaryTerm>;
    $term_children = get_term_children($term->term_id, \'products_categories\');

    if(!empty($term_children)):
        foreach($term_children as $child):
            // Magic for subcategories template
        endforeach;
    else:
        // Magic in case there is no subcategories
    endif;
?>
在每个场景中,您都必须使用$child->term_id 或者$term->term_id.

请注意,如果一个产品有两个子类别,它将在同一页上显示两次。

参考参见法典:https://codex.wordpress.org/Function_Reference/get_term_children

EDIT

加载当前类别(在category.php 模板)以替换<yourPtrimaryTerm> 在我的第一个示例中,您应该能够使用以下代码段:

<?php
    $termTitle = single_cat_title( \'\', false );
    $termId = get_cat_ID($termTitle);
    $term = get_term($termId, \'products_categories\');
?>

SO网友:Mohamed Omar

试试这个,它应该可以工作,并且确保您可以编辑查询参数以满足您的需要。&复制;正确的过去代码html 标签,它应该工作。检查一下,如果有用的话,我会尽量让它更干净。

    <?php
        $this_category = get_category($cat);
        $cats=get_categories(array(\'hide_empty\' => \'0\', \'parent\'=>$this_category ->cat_ID,\'order\'=> \'ASC\',\'depth\'=> \'1\'));

        if(!empty($cats)){
            foreach ($cats as $cat) {
                $newargs = array(
                        \'tax_query\' => array(
                            array(
                                \'taxonomy\' => \'category\',
                                \'field\' => \'name\',
                                \'terms\' => $cat->name,
                                        )
                             )
                          );
                echo\'<div>\';
                echo \'<span class="subhead-title">\'.$cat->name.\'</span>\';
                $nopostsparent= new wp_query( $newargs );
                if ($nopostsparent->have_posts()) {
                    echo \'<ul class="cat-arc-links">\';
                while ($nopostsparent->have_posts() ){
                    $nopostsparent->the_post();
                       echo\'<li><a href="\'. get_permalink() .\'" class="product-link">\'.get_the_title().\'</a></li>\';
                }
                wp_reset_query();
                echo \'</ul></div>\';
             }
            }
        }else{
        $newargs = array(
                \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'category\',
                        \'field\' => \'name\',
                        \'terms\' => $this_category->name,
                    )
                  )
            );
            $nopostsparent= new wp_query( $newargs );
            if ($nopostsparent->have_posts()) {
              echo \'<ul class="cat-arc-links">\';
            while ($nopostsparent->have_posts() ){
             $nopostsparent->the_post();
             echo\'<li><a href="\'. get_permalink() .\'" class="product-link">\'.get_the_title().\'</a></li>\';
            }
            wp_reset_query();
                echo \'</ul></div>\';
            }
        }
    ?>

SO网友:Saiful Islam

对您的需求有一些想法。检查以下代码。。。不要忘记更新分类法和自定义post-type bellow代码。

$parent= get_queried_object()->term_id;
$taxonomy = get_categories(\'child_of=\'.$parent. \'&hide_empty=0&echo=0&taxonomy=custom_taxonomy\');

if(count($taxonomy) > 0){

     foreach ( $taxonomy as $row ) { 
        echo \'<span>\'.$row->name.\'</span>\';
        if(count($child)==$i){
            echo \'<ul class="cat-arc-links">\';
            $args=array( \'post_type\' => \'custom_post\',\'posts_per_page\'=>1,\'tax_query\'  => array( array(\'taxonomy\' => \'custom_taxonomy\',\'terms\' =>$row->term_id, \'field\' => \'id\' )) );
            $second_query = new WP_Query( $args );
                if ($second_query->have_posts()) :
                    while ($second_query->have_posts()) : $second_query->the_post();
                        echo \' <li><a href="\'.get_permalink(get_the_ID()).\'" class="product-link">\'.get_the_title().\'</a></li>\';
                    endwhile; 
                endif; wp_reset_query();
            echo \'</ul>\';       
        }
     }

}else{
    echo \'<ul class="cat-arc-links">\';
    $args=array( \'post_type\' => \'custom_post\',\'posts_per_page\'=>1,\'tax_query\'  => array( array(\'taxonomy\' => \'custom_taxonomy\',\'terms\' =>get_queried_object()->term_id, \'field\' => \'id\' )) );
    $second_query = new WP_Query( $args );
        if ($second_query->have_posts()) :
            while ($second_query->have_posts()) : $second_query->the_post();
                echo \' <li><a href="\'.get_permalink(get_the_ID()).\'" class="product-link">\'.get_the_title().\'</a></li>\';
            endwhile; 
        endif; wp_reset_query();
    echo \'</ul>\';       

}