Problem: I’m trying to echo tags to list items but only manage to echo the “else statement”.
Since this is about tags for pages I have added support for pages to show tags in functions.php:
// add tag support to pages
function tags_support_all()
{
register_taxonomy_for_object_type('post_tag', 'page');
}
// ensure all tags are included in queries
function tags_support_query($wp_query)
{
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}
// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');
The actual code for the tags to be echoed:
<div class="tag-bar">
<ul class="nav">
<?php
$post_tags = get_the_tags();
if (!empty($post_tags)) {
foreach ($post_tags as $tag) :
if ($tag->name === 'pc-installation') :
echo '<li class="nav-item"><a class="nav-link sgit-nav-link sgit-nav-link--dator-support" href="' . get_tag_link($tag) . '">' . $tag->name . '</a></li>';
elseif ($tag->name === 'programvara') :
echo '<li class="nav-item"><a class="nav-link sgit-nav-link sgit-nav-link--installation" href="' . get_tag_link($tag) . '">' . $tag->name . '</a></li>';
elseif ($tag->name === 'skrivare') :
echo '<li class="nav-item"><a class="nav-link sgit-nav-link sgit-nav-link--skrivarsupport" href="' . get_tag_link($tag) . '">' . $tag->name . '</a></li>';
elseif ($tag->name === 'natverk') :
echo '<li class="nav-item"><a class="nav-link sgit-nav-link sgit-nav-link--network" href="' . get_tag_link($tag) . '">' . $tag->name . '</a></li>';
else :
echo '<pre>';
print_r($tag->name);
echo '</pre>';
endif;
endforeach;
} ?>
</ul>
</div><!-- End .tag-bar -->
The three top most tags are assigned to the page (“pc-installation”, “programvara” and “skrivare”) and should be echoed.
This is the expected output:

However all being output is the else statement – why is that? I have checked the tag names correlate to my code.
I would really appreciate help on this one. Thank you!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I solved it. Should be using elseif($tag->slug) instead of elseif($tag->name) or else it will never be true.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0