How to display the tags?

I create infobook box where category as publisher and tags as genre. This is my code:

<table>
<?php
    $my_publisher = get_the_category();
    $my_genre = get_the_tags();

if( ! empty( $my_publisher[0] ) ) {
        echo '<tr><td align="right" class="infobook"><b>Publisher</td><td align="center" class="infobook">:</td></b><td class="infobook"> <a href="'.get_category_link($my_publisher[0]->term_id ).'">'.$my_publisher[0]->cat_name.'</a></td></tr>';
            }

if( ! empty( $my_genre ) ) {
        echo '<tr><td align="right" class="infobook"><b>Genre</td><td align="center" class="infobook">:</td></b><td class="infobook"> ' .$my_genre. ' </td></tr>';
            }
?>
</table>

Category as publisher is work, but tags as genre not working (do not show).
What should I do to fix the code?

Related posts

1 comment

  1. get_the_tags() returns an array of objects. Each object has a “name” property. If you want to show all the tags assigned to the post, you will have to loop through them and echo the name property of each tag. If you want to display a link for each tag, you will have to use the get_tag_link() function and pass in the tag object to get the link.

    I modified the code; you can see it below. I am using a array to store the links and then I implode it using comma as a separator.

    <table>
    <?php
    $my_publisher = get_the_category();
    $my_genre = get_the_tags();
    
    if( ! empty( $my_publisher[0] ) ) {
        echo '<tr><td align="right" class="infobook"><b>Publisher</td><td align="center" class="infobook">:</td></b><td class="infobook"> <a href="'.get_category_link($my_publisher[0]->term_id ).'">'.$my_publisher[0]->cat_name.'</a></td></tr>';
    }
    
    if( ! empty( $my_genre ) ) {
        $tag_links = array();
        foreach($my_genre as $tag) {
            $tag_links[] = '<a href="'.get_tag_link($tag).'">'.$tag->name.'</a>';
        } 
        echo '<tr><td align="right" class="infobook"><b>Genre</td><td align="center" class="infobook">:</td></b><td class="infobook"> ' . implode(',', $tag_links) . ' </td></tr>';
    }
    ?>
    </table>
    

Comments are closed.