Question

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'-->>
Was it helpful?

Solution

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;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top