0

I've custom post "products" with standart categories and subcategories. Now I need to display a list of products on category page, that current category contains.

If category don't have subcategories I need to display this:

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

And if category contains sub categories display this:

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

How can I modyfy my categories.php to make my case done? Thanks in advance!

1

Thanks to all! Here is my solution which worknig well

<?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>";
}
?>
0

Try This, it should work and sure you can edit the query arguments to fit your needs. copy&past code with proper html tags and it should work. Check and if it worked i will try to make it more cleaner.

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

Once you have the primary category (the one that's loaded to the categories.php template), you can use the function get_term_children() to get all of its children.

It will return false or an empty array if it doesn't have children. In the example below, I assumed that your product's taxonomy is name products_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;
?>

In each scenario, you will then have to create a new WP_Query using the $child->term_id or the $term->term_id.

Note that, if a product is available in two subcategories, it will be displayed twice on the same page.

See Codex for reference: https://codex.wordpress.org/Function_Reference/get_term_children

EDIT

To load the current category (when in the category.php template) as to replace the <yourPtrimaryTerm> from my first example, you should be able to use this snippet:

<?php
    $termTitle = single_cat_title( '', false );
    $termId = get_cat_ID($termTitle);
    $term = get_term($termId, 'products_categories');
?>
  • What I need to write in $term = <youPrimaryTerm>; if I need do this stuff with all terms (categories and subcategories) that I have? – Alex P. Jan 10 '17 at 8:23
  • I just added a little snippet that should allow you to complete the code above. Don't hesitate if you need anything else! – Marc-Antoine Parent Jan 12 '17 at 7:21
0

Got some ideas for your requirement. Check below code... don't forgot to update taxonomy and custom post type bellow code .

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.