3

the_tags by default is displaying as URL. I need to display it as plain text to insert inside HTML attribute.

<?php 
    $tag = the_tags('');
?>

<div class="image-portfolio" data-section="<?php echo $tag ?>">
  • Can't you simply use get_the_tags()? – kero Jul 24 '17 at 8:12
  • i tried by had this error: Object of class WP_Term could not be converted to string.. This how i used it: $tag = get_the_tags(); foreach($tag as $tags) { echo $tags; } – Viktor Jul 24 '17 at 8:16
4

I'm glad you figured it out, but below is a quick semantic alteration (swapping your use of $tag singular and $tags plural will help it read closer to what it is doing; functionally it is no different).

I also added cases for inside and outside the Loop, and a conditional so you don't end up trying to foreach loop when there are no tags.

Commented:

//We are inside the Loop here. Other wise we must pass an ID to get_the_tags()

    //returns array of objects
    $tags = get_the_tags();

    //make sure we have some
    if ($tags) {

        //foreach entry in array of tags, access the tag object
        foreach( $tags as $tag ) {

           //echo the name field from the tag object 
           echo $tag->name;
        }
    }

Inside Loop

    $tags = get_the_tags();

    if ($tags) {

        foreach( $tags as $tag ) {

            echo $tag->name;
        }
    }

Outside Loop:

    $id = '10';

    $tags = get_the_tags( $id );

    if ($tags) {

        foreach( $tags as $tag ) {

            echo $tag->name;
        }
    }

Further

Inside the loop, get_the_tags() uses the current post id. Outside of the loop, you need to pass it an id. Either way, it returns an array of WP_TERM objects for each term associated with the relevant post.

[0] => WP_Term Object
    (
        [term_id] => 
        [name] => 
        [slug] => 
        [term_group] => 
        [term_taxonomy_id] => 
        [taxonomy] => post_tag
        [description] => 
        [parent] => 
        [count] => 
    )

You could access each value the same way as above, i.e.:

    $tags = get_the_tags(); 
    if ($tags) {
        foreach( $tags as $tag ) {

            echo $tag->term_id;
            echo $tag->name;
            echo $tag->slug;
            echo $tag->term_group;
            echo $tag->term_taxonomy_id;
            echo $tag->taxonomy;
            echo $tag->description;
            echo $tag->parent;
            echo $tag->count;
        }
    }
  • Thanks for this - why do we need to do $tag->name and can't do $tag["name"]? (I'm learning PHP right now and that seems to be a legit way to return an object key's value, unless I'm missing something?) – BruceWayne May 10 at 19:57
1

Ok i figure it out

$tag = get_the_tags();
foreach($tag as $tags) {
    echo $tags->name;
}
1

I love single lined codes, don't you?

<?php echo implode(' ,' wp_get_post_tags( get_the_ID(), array('fields' => 'names') ) ); ?>
0

This will display the tags as a comma-space separated string:

echo join(', ', array_map(function($t) { return $t->name; }, get_the_tags()));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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