I'm trying to create the single.php post page for a WordPress blog. I have used the Loop to pull through the actual content but would also like to show the tags (if there are any)!
The first code snippet works perfectly and displays everything I want it to, but is it the best way to write it? Can I use 2 if statements next to each other or is this bad practice? I have tried both methods: 2 IF statements works but 1 IF statement doesn't...see below!
Thanks in advance!
Working Code Snippet (using 2 IF statements)
<p class="single-date"><?php echo get_the_date();?></p>
<?php the_content();?>
<?php endwhile; else: endif;?>
<?php
$tags = get_the_tags();
if( $tags ) :
foreach( $tags as $tag ) : ?>
<div class="single-tag">
<a href="<?php echo get_tag_link( $tag->term_id);?>">
<?php echo $tag->name;?></a>
</div>
<?php endforeach; endif;?>
</div>
Invalid Code Snippet (attempt to use only 1 IF statement)
Here, I get the following warning: Invalid argument supplied for foreach()
<p class="single-date"><?php echo get_the_date();?></p>
<?php the_content();?>
<?php endwhile;?>
<?php $tags = get_the_tags();
foreach( $tags as $tag ) : ?>
<div class="single-tag">
<a href="<?php echo get_tag_link( $tag->term_id);?>">
<?php echo $tag->name;?></a>
</div>
<?php endforeach; endif; ?>
</div>
Comments
Aucun commentaire:
Enregistrer un commentaire