Support » Themes and Templates » Get cat and products links

  • Hi All,

    I am trying to get a list of Categories and list of Products under each category to be my sidebar for my custom post type called products.

    So here is what I need:-

    Cat name
    – Product one
    – Product two

    Cat two name
    – Product one
    – Product two

    I need this sidebar on my archive-products.php page and my single-products.php page.

    Here is what I have so far but not sure how to make the links work for the categories and the products:-

    <ul>
    <?php
    $taxonomy = 'productscat';
    $tax_terms = get_terms($taxonomy);
    
    foreach ($tax_terms as $tax_term) {
    
    $catID = $tax_term->term_id;
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    //echo get_category_link( $tax_term->term_id);
    
    //print_r($tax_term);
    
    $args = array(
    	'post_type' 		=> 'products',
    	'productscat'	   => $tax_term->slug,
    	'posts_per_page'   => -1
    );
    
    $posts=get_posts($args);
    
    if ($posts)
    {
    ?>
    <li>
    	<a href="#"><?php echo $tax_term->name; ?></a>
    	<ul>
    		<?php
    			foreach($posts as $post)
    			{
    				setup_postdata($post);
    
    				//print_r($post);
    
    				$src	  = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'productfeature' );
    				$title	= $post->post_title;
    				//$item    = get_post_meta($post->ID, 'item', TRUE);
    
    		?>
    		<li><a href="<?php the_permalink() ?>"> <?php echo $title; ?></a></li>
    		<?php
    			}
    		?>
    	</ul>
    	<?php
    		}
    	}
    	?>
    </li>
    </ul>
  • The topic ‘Get cat and products links’ is closed to new replies.