Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get all Terms for the current product from his taxonomy WordPress

  • SOLVED

I have enabled a custom post type called <strong>products</strong> and a taxonomy called <strong>products-category</strong>.

In the template single-products.php, I have a section where I want to display all the posts from current taxonomy.

Here I have the code that actually outputs all the products from all categories:




<ul class="clearfix jcarousel-skin-tango">
<?php $loop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => 60 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="item">
<div class="box">
<?php
if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('product-thumbnail'); ?></a>
<?php } else { ?>
<a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory') ;?>/i/post-no-img.png" alt=" "/></a>
<?php }
?>
</div>
<p class="title"><?php the_title() ?></p>
<div class="color">&nbsp;</div>
<div class="plus_bg"><a href="<?php the_permalink() ?>"></a></div>
</li>

<?php endwhile; ?>
</ul>




I also managed to call the current post taxonomy title successfully. Hope it can help.
<?php $terms = get_the_terms($post->ID, 'product-category');foreach($terms as $term){echo $term->name;} ?>

Again, on the single view I want only the posts from current post's taxonomy to show up.
If I have taxonomies such as: <strong>Furniture</strong>, <strong>Lighting</strong> etc. and I have a post in <strong>Furniture</strong> taxonomy, I want to show dynamically the posts from <strong>Furniture</strong> only.

Let me know if you have any questions.

Answers (4)

2011-12-27

Navjot Singh answers:

Exactly same question has been asked here at WPQuestions. [[LINK href="http://wpquestions.com/question/showLoggedIn/id/2611"]]Check here[[/LINK]]. The solution posted by Dylan should work for you also.

Just replace <strong>firma</strong> with <strong>product-category</strong>.


Lucian Florian comments:

thank you. Can you format the code for me so I can copy and paste?


Navjot Singh comments:

<?php

$product_terms = get_the_terms( 0, 'product-category' );

if ( $product_terms )

$product_term_slugs = wp_list_pluck( $product_terms, 'slug' );

$args = array(

'post_type' => 'products',

'order' => 'ASC',

'posts_per_page' => -1,

'tax_query' => array(

array(

'taxonomy' => 'product-category',

'field' => 'slug',

'terms' => $product_term_slugs

)

)

);?>



<?php $my_query = new WP_Query(); $my_query->query($args); ?>


<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>

<?php endwhile; ?>

<?php wp_reset_postdata(); ?>


Lucian Florian comments:

your solution works well. thanks

2011-12-27

Albert Shala answers:

This how i setup mine up, hope it's useful:


<!-- Display news -->
<?php if ( is_page('news')) { ?>
<?php query_posts( array( 'categorys' => 'news', 'showposts' => 12 ) ); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>


<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?>


<!-- -->

<!-- Display CGI News -->
<?php } elseif ( is_page('cgi-news')) { ?>
<?php query_posts( array( 'categorys' => 'cgi-news', 'showposts' => 12 ) ); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>


<h3><?php the_title(); ?></h3>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?>
<?php } else { ?>
<!-- -->

<!-- Display Publications -->
<?php query_posts( array( 'categorys' => 'publications', 'showposts' => 12 ) ); ?>
<h1><?php echo $sub_title; ?></h1>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<?php endwhile; ?>

<?php } ?>

2011-12-27

Just Me answers:

In other words, you need to "catch" the current product's product-category and add it to the query.

This part of your code catches the product-category

<?php $terms = get_the_terms($post->ID, 'product-category'); ?>

<?php $loop = new WP_Query( array( 'post_type' => 'products', 'posts_per_page' => 60, 'product_category' => $terms ) ); ?>


Lucian Florian comments:

This could have been the simplest, and with minimal code but unfortunately doesn't work.

2011-12-27

Eli Scheetz answers:

Try this:

<?php
$product-category=array();
$terms = get_the_terms($post->ID, 'product-category');
foreach($terms as $term)
$product-category[] = $term->name;
?>

<ul class="clearfix jcarousel-skin-tango">

<?php $loop = new WP_Query( array('product-category' => $product-category, 'post__not_in' => array( $post->ID ), 'post_type' => 'products', 'posts_per_page' => 60 ) ); ?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<li class="item">

<div class="box">

<?php

if ( has_post_thumbnail() ) { ?>

<a href="<?php the_permalink() ?>"><?php the_post_thumbnail('product-thumbnail'); ?></a>

<?php } else { ?>

<a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_directory') ;?>/i/post-no-img.png" alt=" "/></a>

<?php }

?>

</div>

<p class="title"><?php the_title() ?></p>

<div class="color">&nbsp;</div>

<div class="plus_bg"><a href="<?php the_permalink() ?>"></a></div>

</li>



<?php endwhile; ?>

</ul>