Ask your WordPress questions! Pay money and get answers fast! (more info)

echo term name inside loop WordPress

  • SOLVED


I can't seem to get anything to work, can anyone help me with a quick fix.

Please see my loop below...


<?php $footerScooter = new WP_Query(array(

'post_type' => 'on-road',
'group' => 'scooter',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'app_visibility',
'value' => '1',
'compare' => '=='
)
)

));

if ($footerScooter->have_posts()) : ?>

<ul>

<li><?php echo get_the_terms($post->ID, 'group') ?></li>
<li>

<ul>

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

<li><a href="" title=""><?php the_title(); ?></a></li>

<?php endwhile; ?>

</ul>

</li>

</ul>





As you can see in my first <li> I am trying to output my taxonomy term. But I just get nothing, and i've tried lots.

My taxonomy is called 'Group' - and I simply just want to echo the name of the terms asigned to this query in 'group'


Thanks
Josh

Answers (3)

2012-10-23

Martin Pham answers:

Hi Josh,
Because get_the_terms(...) function return array of term objects on success. So, you can check with <strong>print_r</strong> or <strong>var_dump</strong>

<li><?php print_r(get_the_terms($post->ID, 'group')) ?></li>


AND
some parameters of the WP_Query is <strong>wrong</strong> :
<strong>not used:</strong>
'group' => 'scooter'

Use (API/Filter) : [[LINK href="http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses"]]http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses[[/LINK]]

<strong>not used:</strong>
'compare' => '=='

Use:
'compare' => '='

--------
With your problem, try to use this function:

function group_tax_term_link() {
global $post;
$tax_name = 'Group';
$return = '';
$terms = get_the_terms( $post->ID, $tax_name );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term )
$out[] = '<a href="' .get_term_link($term->slug, $tax_name) .'">'.$term->name.'</a>';
$return = join( ', ', $out );
}
return $return;
}


use inside <strong>while</strong> (loop):

<?php $footerScooter = new WP_Query(array(
'post_type' => 'on-road',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'app_visibility',
'value' => '1',
'compare' => '='
)
)
));

if ($footerScooter->have_posts()) : ?>
<ul>
<?php while ($footerScooter->have_posts()) : $footerScooter->the_post(); ?>
<li><?php echo group_tax_term_link(); ?></li>
<li><a href="" title=""><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>


Josh Cranwell comments:

Thanks for you advice and tips.

I gave up in the end could not get it to work.

I tried all.


Many Thanks

2012-10-23

Arnav Joy answers:

try this inside loop


$term_list = wp_get_post_terms( get_the_ID(), 'Group', array("fields" => "names"));


echo $term_list[0];


Josh Cranwell comments:

Thanks arnav I think there is error - seems to break?

2012-10-23

Inam Hassan answers:

you probably should be using wp_get_post_terms()
which goes just like this:
<?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ) ?>

$args is optional (and so is $taxonomy but you need it on this case)