admin管理员组

文章数量:1297086

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!

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!

Share Improve this question edited Apr 1, 2021 at 9:28 Seblito asked Mar 31, 2021 at 11:53 SeblitoSeblito 297 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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

本文标签: loopTags on page (not post) returns nothingwhy Improved clarified question