PUSTAKAKORAN.COM PUSTAKAKORAN.COM - 1 month ago 9
PHP Question

Display custom taxonomy only if is not empty

I have BOOKS INFOBOX like this:

<h4 style="margin:0 8px 6px 0px; padding-left:20px;">Data Buku</h4>
<table>
<?php
$my_title = get_the_title();
$my_date = get_post_meta( get_the_ID(), 'date', true);
$my_penulis = get_post_meta( get_the_ID(), 'penulis', true);
$my_isbn = get_post_meta( get_the_ID(), 'isbn', true);
$my_publisher = get_the_category();
$my_author = get_the_tags();

if( ! empty( $my_title ) ) {
echo '<tr><td align="right" class="style" width="210"><b>Title</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_title . '</td></tr>';
}
if( ! empty( $my_date ) ) {
echo '<tr><td align="right" class="style"><b>Release Date</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_date . '</td></tr>';
}
if( ! empty( $my_isbn ) ) {
echo '<tr><td align="right" class="style"><b>ISBN</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_isbn . '</td></tr>';
}
if( ! empty( $my_publisher[0] ) ) {
echo '<tr><td align="right" class="style"><b>Publisher</td><td align="center" class="style">:</td></b><td class="style"> <a href="'.get_category_link($my_publisher[0]->term_id ).'">'.$my_publisher[0]->cat_name.'</a></td></tr>';
}
if( ! empty( $my_author ) ) {
$tag_links = array();
foreach($my_author as $tag) {
$tag_links[] = '<a href="'.get_tag_link($tag).'">'.$tag->name.'</a>';
}
echo '<tr><td align="right" class="style"><b>Author</td><td align="center" class="style">:</td></b><td class="style"> ' . implode(', ', $tag_links) . ' </td></tr>';
}
?>


then I create genre field used by Simple Taxonomy Plugin, and I get code to display like this:

<?php the_terms( $post->ID, 'genre', 'Genre: ', ', ', ' ' ); ?>


My problem, how I put genre field code in BOOKS INFOBOX, and if blank data so genre field do not show?

Answer

Instead using the_terms() function, you should use get_the_term_list() function, to include it in your echoed html code. Then I have added the display at the end of your Books Infobox (You can move it where you want, to feet your needs).

Here is your code with some changes:

<h4 style="margin:0 8px 6px 0px; padding-left:20px;">Data Buku</h4>
<table>
<?php
    $my_title = get_the_title();
    $my_date = get_post_meta( get_the_ID(), 'date', true);
    $my_penulis = get_post_meta( get_the_ID(), 'penulis', true);
        $my_isbn = get_post_meta( get_the_ID(), 'isbn', true);
    $my_publisher = get_the_category();
    $my_author = get_the_tags();

    // Here we save the "Genre" terms in a variable.
    $genre_terms = get_the_term_list( $post->ID, 'genre', 'Genre: ', ', ', ' ' );

    if( ! empty( $my_title ) ) {
        echo '<tr><td align="right" class="style" width="210"><b>Title</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_title . '</td></tr>';
    }
    if( ! empty( $my_date ) ) {
        echo '<tr><td align="right" class="style"><b>Release Date</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_date . '</td></tr>';
    }
    if( ! empty( $my_isbn ) ) {
        echo '<tr><td align="right" class="style"><b>ISBN</td><td align="center" class="style">:</td></b><td class="style"> ' . $my_isbn . '</td></tr>';
    }
    if( ! empty( $my_publisher[0] ) ) {
        echo '<tr><td align="right" class="style"><b>Publisher</td><td align="center" class="style">:</td></b><td class="style"> <a href="'.get_category_link($my_publisher[0]->term_id ).'">'.$my_publisher[0]->cat_name.'</a></td></tr>';
        }
    if( ! empty( $my_author ) ) {
        $tag_links = array();
        foreach($my_author as $tag) {
            $tag_links[] = '<a href="'.get_tag_link($tag).'">'.$tag->name.'</a>';
        } 
        echo '<tr><td align="right" class="style"><b>Author</td><td align="center" class="style">:</td></b><td class="style"> ' . implode(', ', $tag_links) . ' </td></tr>';
    }
    // Here "Genres" are displayed if not empty.
    if( ! empty( $genre_terms ) ) {
        '<tr><td align="right" class="style"><b>Author</td><td align="center" class="style">:</td></b><td class="style"> ' . $genre_terms . ' </td></tr>';          
    }
?>
</table>

This should work.