Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Summary:
How can I get the name and the permalink of the object returned by wp_get_object_terms()?

Detailled:
I created a custom post type called "ge_zielgruppe" and a taxonomy called "ge_zielgruppe_taxonomy". The latter can be attached to posts and to "ge_zielgruppe" post types.

On the single page of "ge_zielgruppe" I want to show the last few posts tagged with the same "ge_zielgruppe_taxonomy". I achieved this with

<?php
$theZielgruppe = wp_get_object_terms($post->ID, 'ge_zielgruppe_taxonomy');
$zielgruppe = new WP_Query(array('ge_zielgruppe_taxonomy' => $theZielgruppe->slug));
$zielgruppe->query('showposts=10');
if ($zielgruppe->have_posts()) :
    while ($zielgruppe->have_posts()) :
        $zielgruppe->the_post();
?>
<<--archive-stuff-->>
<?php
    endwhile;
endif;
?>

This part works (however, I don't know if it's elegant).

Now I'd like to put a link right after these 10 posts, looking like this

<a href="<<--permalink to archive of 'ge_zielgruppe_taxonomy'-->>" rel="bookmark" title="More posts for <<--Name of 'ge_zielgruppe_taxonomy'-->>; ">More posts for <<--Name of 'ge_zielgruppe_taxonomy'-->></a>

So how do I get

  1. <<--permalink to archive of 'ge_zielgruppe_taxonomy'-->> and
  2. <<--Name of 'ge_zielgruppe_taxonomy'-->>
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

To fetch the archive url for that taxonomy term, use something like this (I'm using your naming conventions above, and assuming that $theZielgruppe is a term object.

$url = get_term_link( $theZielgruppe, 'ge_zielgruppe_taxonomy' );

To get the name, just use

$theZielgruppe->name

Is that what you're looking for?

EDIT

The link above would then look like this:

<a href="<?php echo get_term_link( $theZielgruppe, 'ge_zielgruppe_taxonomy' ); ?>" rel="bookmark" title="More posts for <?php echo $theZielgruppe->name; ?>; ">More posts for <?php echo $theZielgruppe->name; ?></a>

EDIT 2

wp_get_object_terms() returns an array of terms. If you changed each use of $theZielgruppe to $theZielgruppe[0] to use the first term that the current ge_zielgruppe relates to. A warning, though: wp_get_object_terms() can return as either an empty array or as a WP_Error. You might want to change your code to check for that:

<?php
$theZielgruppe = wp_get_object_terms($post->ID, 'ge_zielgruppe_taxonomy');
if( !empty( $theZielgruppe ) && !is_wp_error( $theZielgruppe ) ):
    $theZielgruppe = $theZielgruppe[0];
    $zielgruppe = new WP_Query(array('ge_zielgruppe_taxonomy' => $theZielgruppe->slug));
    $zielgruppe->query('showposts=10');
    if ($zielgruppe->have_posts()) :
        while ($zielgruppe->have_posts()) :
            $zielgruppe->the_post();
    ?>
    <<--archive-stuff-->>
    <?php
        endwhile;
    endif;
    ?>
    <a href="<?php echo get_term_link( $theZielgruppe, 'ge_zielgruppe_taxonomy' ); ?>" rel="bookmark" title="More posts for <?php echo $theZielgruppe->name; ?>; ">More posts for <?php echo $theZielgruppe->name; ?></a>
    <?php
endif;
?>
share|improve this answer
    
Thank you! I didn't find get_term_link() yet, thanks for that. However $theZielgrupe->name doesn't work. Also <?php echo $theZielgruppe->name;?> leaves me without any output, although <?php print_r($theZielgruppe);?> works... –  Marcel Apr 8 '11 at 20:23
    
What does it say when you use print_r($theZielgruppe);? –  John P Bloch Apr 8 '11 at 20:26
    
output of print_r($theZielgruppe);: Array ( [0] => stdClass Object ( [term_id] => 6 [name] => studierende [slug] => studierende [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => ge_zielgruppe_taxonomy [description] => [parent] => 0 [count] => 3 ) ) please also note I updated my original question and added my call of wp_get_object_terms() in the first lines of the first code box. –  Marcel Apr 8 '11 at 20:30
1  
Better question is how do you get $theZielgruppe? –  John P Bloch Apr 8 '11 at 20:36
1  
Glad to help out! –  John P Bloch Apr 8 '11 at 20:55
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.