Search code examples
phpwordpressechopermalinkscustom-taxonomy

Wordpress PHP - Add the_permalink inside a variable then echo


I've got this code below that gets a specific category (from a taxonomy) then displays its sub-categories.

I'm trying to figure out how to add a href link to it, linking to the permalink in order to go to the archive page for that particular sub-category. So far I have this:

<?php
$terms = get_terms( 'job_listing_category', 'parent=59' );
$count = count($terms);
$link_address = the_permalink();

if ( $count > 0 ){

 foreach ( $terms as $term ) {
   echo "<a href='".$link_address."'><p>" . $term->name . "</p></a>";

 }


}
?>

This line: echo "<a href='".$link_address."'><p>" . $term->name . "</p></a>"; seems to be correct, however I need to reference the $link_address somewhere I assume...

I thought I could get the permalink of the sub-category by adding the line: $link_address = the_permalink();

However, this doesn't seem to work and I'm not sure why..

Any help greatly appreciated :)


Solution

  • You can get term link with get_term_link() and you can pass in the term object. You could do:

    foreach ( $terms as $term ) {
      echo "<a href='" . get_term_link($term) . "'>" . $term->name . "</a>";
    }