Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to be able to conditionally hide the author's name on the Single.php if the Category name is equal to "News" and otherwise if the category is something else hide it. How do I go about doing this? The code I have below does not seem to work:

            <!-- header.php -->
        <?php get_header(); ?>
        <!-- /header.php -->

        <!-- 960 16 Column Grid -->
        <section class="container_16">

        <!-- Top Pagination -->
        <?php include('includes/top-pagination.php'); ?>
        <!-- /Top Pagination -->

        <div class="clear"></div>

            <!-- Post -->
            <article class="grid_10 single-post-content margin-top-60">

                <!-- Post Loop -->
                <?php if (have_posts()) while (have_posts()) : the_post(); ?>

                    <!-- Clear -->
                    <div class="clear"></div>
                    <!-- /Clear -->

                    <!-- Post Title -->
                    <h1><?php the_title(); ?></h1>
                    <!-- /Post Title -->

                    <!-- Featured News Meta -->
                    <?php if(is_category('news')) { ?>
                    <p class="featured-news-post-meta"><?php echo get_the_date('m.d.Y'); ?></p>
                    <?php  } else { ?>
                    <p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date('m.d.Y'); ?></p>
                    <?php } ?>
                    <!-- /Featured News Meta -->

                    <!-- Post Content -->
                    <?php the_content(); ?>
                    <!-- /Post Content -->

                    <?php //endif; ?>

                    <!-- Divider -->
                    <div class="separator"></div>
                    <!-- /Divider -->

                    <!-- Featured News Social Links -->
                    <?php include('includes/social.php'); ?>
                    <!-- /Featured News Social Links -->

                    <!-- Comments -->
                    <?php //comments_template( '', true ); ?>
                    <!-- /Comments -->

                    <?php endwhile; ?>
                    <!-- /Post Loop -->

            </article>
            <!-- /Post -->

            <!-- What's Happening -->
            <div class="margin-top-60">
                <?php include('includes/whats-happening.inc.php'); ?>
            </div>
            <!-- /What's Happening -->

        </section>
        <!-- /960 16 Column Grid -->

        <!-- sidebar.php -->
        <?php get_sidebar(); ?>
        <!-- /sidebar.php -->

        <!-- footer.php -->
        <?php get_footer(); ?>
        <!-- /footer.php -->
share|improve this question

2 Answers 2

up vote 2 down vote accepted

is_category returns true if the current page being viewed is a category term page (i.e. a list of posts belonging to some term) and false otherwise. So inside single.php it will always return false.

If you want to check if a the current post in the Loop belongs to some category term, then you need to use has_term().

For example:

 <?php if( has_term('news','category') ) { ?>
      <p class="featured-news-post-meta"><?php echo get_the_date('m.d.Y'); ?></p>
 <?php  } else { ?>
      <p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date('m.d.Y'); ?></p>
  <?php } ?>
share|improve this answer

You are using is_category() where I believe you want in_category()

is_category() is true on a category archive page, in_category() returns true if a post belongs to a particular or array of categories.

EDIT in_category() is just a wrapper for the generic has_term(), see Stephen's answer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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