Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If Else On Php WordPress

<?php
$genre = get_the_term_list($post->ID, 'genre', '<span class="genre">', ', ', '</span>');
$tags = get_the_term_list($post->ID, 'tag-series-movies','<span class="genre">', ', ', '</span>');

if($genre,$tags)
    echo '<li>Genre: '. $genre . '</li>';
    echo '<li>Tags: '. $genre . '</li>';
else {
    echo '';
}
?>

Hi. Guys whats wrong of my code? though the code is working 50%. the $genre is working but the else is not working. please help. i want to show the nothing when no genres is not available.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Change if($genre == $genre) to if($genre){

get_the_term_list returns the list or false so you just need to check that $genre is a truthy value

In your current code when $genre is false you are checking if(false == false) which always returns true

EDIT:

you can simplify your code like this

if($genre){
    echo '<li>Genre: '. $genre . '</li>';
}
if($tags){
    echo '<li>Tags: '. $tags . '</li>';
}

There’s no need for the else statement as you’re not echoing anything

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading