0

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:

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!

1 Answer 1

1

I solved it. Should be using elseif($tag->slug) instead of elseif($tag->name) or else it will never be true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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