• Hi all,

    I am working with a template that displays the custom taxonomy “Tracks” under “Sessions.” These appear as a label, as you can see above. I’d like the labels to link to all the sessions tagged with the same. This is the PHP I am working with:

    <span data-track="<?php echo $track->term_id; ?>" class="label single-session-link btn btn-primaryd<?php if (empty($track->color)) echo " bg-gold"; ?>" <?php if (!empty($track->color)) echo "style='background-color: $track->color;'"; ?>><?php echo $track->name; ?> </span>

    I am trying to add “get permalink” and direct it to the slug but I am really not well versed in writing PHP. Please assist.

    • This topic was modified 6 years, 8 months ago by Jan Dembowski.
    • This topic was modified 6 years, 8 months ago by bcworkz. Reason: code fixed

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    To “linkify” any block of output code using get_permalink(), you wrap it in an anchor tag like so:

    <a href="<?php echo get_permalink(); ?>">
    --- your code block goes here ---
    </a>

    The resulting link isn’t going to have the effect you want though. get_permalink() returns a link to the current post in the loop. In the example page you linked, it will simply link to the same page. You want to link to a taxonomy term archive such as example.com/tracks/my-track/.

    Assuming your $track is a proper term object, replace get_permalink() with get_term_link( $track )

    FYI, it’s helpful if you delimit any code you post in these forums with backticks, or use the code button. If you don’t do so, the forum’s parser corrupts your code. I fixed the code you posted.

Viewing 1 replies (of 1 total)

The topic ‘Adding Link to Label in PHP’ is closed to new replies.