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

Output Taxonomy Contents Dynamically -taxonomy name and its posts WordPress

  • SOLVED

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

I have categories such: Furniture, Lighting, Home Decor etc

On the products.php page, whenever I add a new category, this is the code that needs to show up for each one, including support for custom field:



<div class="carousel custom_field_color">
<div class="category_title">
<h1>Category Title</h1>
</div>
<ul class="clearfix jcarousel-skin-tango">
<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>
</ul>
</div>



I've got a very basic start where I can output dynamically the taxonomy name which works:


<?php

$terms = get_terms('products-category');

foreach ($terms as $term) {

$wpq = array ('taxonomy'=>'products-category',
'term'=>$term->slug);
$query = new WP_Query ($wpq);
$article_count = $query->post_count;

echo $term->name;

}

?>


What I need is take the first block of code and integrate it here, so it will show all the products under that category dynamically, instead of just Taxonomy name.

Let me know if you have any questions.

Answers (3)

2011-12-27

Ivaylo Draganov answers:

Hello,

I guess this is what you are after:
[[LINK href="http://pastebin.com/9Qsjxs5f"]]http://pastebin.com/9Qsjxs5f[[/LINK]]


Lucian Florian comments:

It works well, but now for some reason the jcarousel doesn't load any longer. I believe it has something to do with the loading order. The script is at the end and loads at document ready.

More importantly, how can I output a custom field for each loop?
<div class="carousel custom_field_color">

I just need to be able to take a custom field value.


Lucian Florian comments:

or each carousel div should get a different class, so I can target with CSS and change color.


Lucian Florian comments:

I solved, the carousel problem and I might solved the class with jQuery.

I will vote for you but one more question:
How can I add support for custom fields? I will deal with back-end separately. I want each loop get access to custom field


Ivaylo Draganov comments:

Check out the code again at the same URL. I've added a distinct class to each <div> (the class is actually the term slug). If you want to pull a custom field from a post, just use this code anywrere between <em>while</em> and <em>endwhile</em>:

<?php echo get_post_meta( get_the_ID(), 'custom-field-name', true); ?>


Lucian Florian comments:

Thank you. That helps a lot!

And how I'd go about having the same dynamic slug name class on the single post? It should be parent category slug.


Ivaylo Draganov comments:

<blockquote>And how I'd go about having the same dynamic slug name class on the single post? It should be parent category slug.</blockquote>

I think that [[LINK href="http://codex.wordpress.org/Function_Reference/post_class"]]<?php post_class(); ?>[[/LINK]] would give you the category. You can use it on your list item like so(<em>'item' is an additional custom class</em>):

<li <?php post_class( 'item' ); ?>>


And if for some reason it doesn't output the taxonomy, you can pass it as a parameter to the function:

<li <?php post_class( 'item ' . $term->slug ); ?>>


Or if you don't want all the classes, simply print this:

<li class="item <?php echo $term->slug; ?>">


Lucian Florian comments:

I tried the post class but doesn't shows the same classes.

The next code doesn't do anything.
<?php echo $term->slug; ?>

Maybe I need to repeat the loop on single-products too?


Ivaylo Draganov comments:

Oh, sorry I didn't understand that you want it on the single post template. No, you don't need to repeat the loop. You need to pull the taxonomies for the current post. Try this code in your <em>functions.php</em>:
[[LINK href="http://pastebin.com/QveC9Zh2"]]http://pastebin.com/QveC9Zh2[[/LINK]]


And then call <em>post_class</em> in your template:

<?php post_class(); ?>


Lucian Florian comments:

Thank you, but unfortunately it outputs a class <strong>single-products-category-name</strong>.

I need only <strong>category-name</strong>, that's why I was wondering if there is a way to pull the slug for the parent category.


Ivaylo Draganov comments:

<blockquote>I need only category-name, that's why I was wondering if there is a way to pull the slug for the parent category.</blockquote>

Try this then:
[[LINK href="http://pastebin.com/JygebdLQ"]]http://pastebin.com/JygebdLQ[[/LINK]]


Lucian Florian comments:

I added the last code to the functions.php and on single page I added:
<?php echo $term->slug; ?>

Nothing happens.


Ivaylo Draganov comments:

<blockquote>I added the last code to the functions.php and on single page I added</blockquote>

The code in <em>functions.php</em> is a filter for the <em>post_class</em> function. So use <?php post_class(); ?> in your templates and it will output the terms for every single post (along with a bunch of other classes).

2011-12-27

Mike Van Winkle answers:

http://pastebin.com/VtRzNeQi


Lucian Florian comments:

your code gets me an error: Parse error: syntax error, unexpected ';', expecting ')'


Mike Van Winkle comments:

here's a fix but you can just go with Ivaylo's

http://pastebin.com/Hiic0Mkj


Lucian Florian comments:

still getting an error

2011-12-27

Eli Scheetz answers:

Try this:

<?php
$terms = get_terms('products-category');

foreach ($terms as $term) {

$wpq = array ('taxonomy'=>'products-category',

'term'=>$term->slug);

$query = new WP_Query ($wpq);

$article_count = $query->post_count;

?>
<div class="carousel custom_field_color">

<div class="category_title">

<h1><?php echo $term->name; ?></h1>

</div>

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

<?php $loop = new WP_Query( array('product-category' => $term->name, '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>

</div>
<?php
}
?>


Lucian Florian comments:

syntax error


Eli Scheetz comments:

ooops, I missed a ?>its now fixed if you want to try it again.
Thanks.