Forums

[resolved] Simple PHP Question - Conditional Display of Category (7 posts)

  1. questions4wp
    Member
    Posted 2 years ago #

    I'm pretty new to PHP and I'm having some trouble getting something to work properly. My website has a static home page, and several categories of posts. At the top of every page I display the category name with this code:
    <div class="header3"><?php the_category(', '); ?></div>

    This works just fine for all of the categories. However, I do not want it to display the words "Uncategorized". I can't seem to get the syntax of the If statement correct, but basically what I want is to display the name as long as the category is NOT "Uncategorized".

    Can someone please help me with this simple PHP If statement? I'd really appreciate it!

  2. MichaelH
    Volunteer
    Posted 2 years ago #

  3. questions4wp
    Member
    Posted 2 years ago #

    Thanks! I'm still having a little trouble formatting the command though. Right now I have this:
    <div class="header3"><?php the_category(', '); ?></div>

    but I don't want the the_category(', ') if we're on the home page. According to the page you sent me (thanks again!), because I have a static home page, the is_home() function will return 'true' if I'm on that home page, and 'false' if I'm not. My code so far (which doesn't work) is:

    <div id="container"><div class="header3">
    
    <?php if (is_home()==false) {  the_category(', '); } ?>
    
    </div></div>

    The idea is: it opens the div tags, and then places the category function only if we're not on the home page. It then closes the div tags. Unfortunately, this doesn't work. Any idea why?

  4. banago
    Member
    Posted 2 years ago #

    This is how you do it:

    <?php if (!is_home()) { the_category(', '); } ?>

  5. questions4wp
    Member
    Posted 2 years ago #

    Hmmm, I tried that but it still displays the word uncategorized on the home page

  6. questions4wp
    Member
    Posted 2 years ago #

    Ok so I found one issue, for some reason even though my home page is set to static, if I have

    if (!is_home(){} anything withing the squiggly brackets will show up on every page, home included. For some reason my home page isn't registering as actually being home.

    Is there a way to output the the_category conditionally? like

    if (the_category() != "Uncategorized) {the_category();}

  7. questions4wp
    Member
    Posted 2 years ago #

    sorry for the triple post but I just got it to work...for some reason is_home() didn't do anything for me, but is_front_page() works. Thanks for all the help!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.