WordPress.org

Ready to get started?Download WordPress

Forums

Displaying 'No Tags' when no single tag is inserted (13 posts)

  1. fath
    Member
    Posted 6 years ago #

    Hello WordPress community.

    I have a question. I'm using the_tag() function for displaying tags in my posts. Is it possible for me to make it automatically display as 'No Tags' when no single tag is inserted? Anyone knows how to make it possible?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    I think something like this would work:

    if (the_tags('Tags:','','') === false) echo 'No Tags';

  3. fath
    Member
    Posted 6 years ago #

    Hi Otto42,
    I have tried it but no difference. Still not displaying anything.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Ahh well... This should do it then:

    if (empty(get_the_tags()) echo "No Tags";
    else the_tags('Tags:');
  5. fath
    Member
    Posted 6 years ago #

    Thanks. But, can you show me how should the complete code looks like? It's kinda confusing...

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    What "complete code" are you talking about?

    Just stick what I gave you into your theme wherever you want to display the tags.

  7. fath
    Member
    Posted 6 years ago #

    Done it. But whenever I refresh my website to see the result, blank page appears. Check out http://www.owastudio.com and see whether you can open the website properly...

  8. fath
    Member
    Posted 6 years ago #

    Sorry, have to replaced with normal the_tags() because people complaining they can't visit my website. But that's what happened. Once I put in the above code, my website just showing a blank page.

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Ahh, I left off a closing parentheses. Try this new code.

    if (empty(get_the_tags())) echo "No Tags";
    else the_tags('Tags:');
  10. Ivovic
    Member
    Posted 6 years ago #

    this forum needs a built-in debugger ;)

    (or at least syntax highlighting!)

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    (or at least syntax highlighting!)

    !!!!!

    Yes!

  12. fath
    Member
    Posted 6 years ago #

    Haha. By the way, i paste the new code and still having blank page. Weird?

  13. Dgold
    Member
    Posted 5 years ago #

    For the code Otto42 gave above, I think you would need to add php opening and closing around it, or paste it somewhere that's already in php.

    This code below shows "No Tags here" for posts without a single tag, then it shows the posts that have tags (post title followed by the list of tags). This also illustrates the opening php, IF statement, ELSE statement, and closing php. That can be useful.

    <?php
    $tag = get_the_tags();
    if (! $tag) { ?>
    <p>No tags here. <a href="<?php the_permalink() ?>" rel="bookmark" title="Entry for <?php the_title(); ?>"><?php the_title(); ?></a></p>
    
    <?php } else { ?>
    
    <p>Tags here for <a href="<?php the_permalink() ?>" rel="bookmark" title="Entry for <?php the_title(); ?>"><?php the_title(); ?></a> -  <?php the_tags(' ', ' | ', ''); ?></p>
    
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags