1

I'm trying to print some content in all the posts under a certain author only if a specific tag (default wp taxonomy) is set

the tag is "animali permessi" here is the code

$posttags = 'animali permessi';

if (isset($posttags)) {
$supplemento_animali = '<div class="suppl_animali">'. get_the_author_meta( 'supplemento_animali', $post->post_author ) .'</div>';
}


$content = $content . $titolo_prezzo_include . $locazione  .  $commissione . $consumi . $pulizia_inc . $biancheria_inc . $ac_inc .  $titolo_prezzo_non_include . $pulizia_esc . $tassa_soggiorno . $supplemento_animali . $supplemento_culla . $importo_cauzione . $servizio_spiaggia . $suppl_soggiorni_brevi . $biancheria_esc . $asciugamani_esc . $wi_fi_esc . $ac_esc .  $saldo . '<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti . $titolo_orari . $orari .'</footer>';
}
return $content;
}

The code is fine except for the condition The purpose is printing the "extra charge for pets" only if in each post is set the tag "animali permessi" (pets allowed)

1
  • 1
    isset isn't a WordPress function, it has nothing to do with post tags, it's a PHP function to see if a variable exists/is set
    – Tom J Nowell
    Sep 5, 2023 at 16:37

1 Answer 1

3

isset() checks to see if a variable is set, ie, has a value other than null. The way you're using it will always return true because you've set $posttags to 'animali permessi', which is not null.

I think you're looking for the WordPress function has_tag() instead.

if (has_tag( 'animali permessi', $post ) ) {
    $supplemento_animali = '<div class="suppl_animali">'. get_the_author_meta( 'supplemento_animali', $post->post_author ) .'</div>';
}
1
  • 1
    note that you can use this without $post if it's used inside the loop
    – Tom J Nowell
    Sep 5, 2023 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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