Reputation: 189
I'm a real n00b when it comes to coding in PHP, so I hope you can clear something up for me :)
I've created a portfolio with PODS, which creates custom post-types and taxonomies. What I'd like to achieve is this: Add label(s) to every post, showing the categories the post belongs to, as an image. In my opinion it's the best way for efficiency and liability to make use of the already available data, instead of adding a new field which contains an image / value -> CSS. This would cause unnecessary requests.
So what I did is add a relational field (related to custom taxonomies) as multiple-select. Eventually this should end up with something like:
<span class="value1"></span>
<span class="value3"></span>
<span class="value6"></span>
In that case I'm able to assign an image to it with CSS (as content or background)
I tried in a lot of ways to get the content of the field outputted as separate values, but my knowledge is the bottleneck in this, so I hope someone is willing to point me in the right direction.
This is the code I have at the moment:
<a href="<?php the_permalink(); ?>">
<h1 class="hentry__title"><?php the_title(); ?>
<?php $item_label = get_post_meta( get_the_ID(), 'item_label', false ); ?>
<?php if (!empty($item_label)):
foreach ( $item_label as $cat_single ) { ?>
<span class="item-label"><?php echo ($cat_single); ?></span>
<?php }
endif; ?>
</h1>
</a>
This gives me 2 times the word "array" which looks promising because of the count being correct I guess. But when I take a look at the actual content of the array,
<?php print_r ($item_label); ?>
I get this result:
Array ( [0] => Array ( [term_id] => 93 [name] => Banners [slug] => banners [term_group] => 0 [term_taxonomy_id] => [taxonomy] => portfolio_categorie [description] => [parent] => 0 [count] => 0 [object_id] => [term_order] => [pod_item_id] => 93 ) [1] => Array ( [term_id] => 90 [name] => Websites [slug] => websites [term_group] => 0 [term_taxonomy_id] => 90 [taxonomy] => portfolio_categorie [description] => [parent] => 0 [count] => 2 [object_id] => 2414 [term_order] => 0 [pod_item_id] => 90 ) )
I don't know how to handle this data. I'm sure someone here has an answer for me, so I hope that one would help me out here!
Kindest regards!
Upvotes: 0
Views: 360
Reputation: 2338
At a quick glance it looks like you are close... just need to access the values in the item_label
array. Change your echo
(inside your for loop) statement to:
<?php echo $cat_single['name']; ?>
Upvotes: 1