WordPress.org

Ready to get started?Download WordPress

Forums

the_category display ALL if posted under all categories (4 posts)

  1. aestock
    Member
    Posted 4 years ago #

    Hello,

    I have a site with about 10 or so categories. Some general announcements must be published under all the categories making my post meta very clunky listing out each of the categories with the_category.

    Is there a way to force the_category to display "ALL" (and maybe link back to my blog url) if the post is published under all categories?

    Thank you!

  2. vtxyzzy
    Member
    Posted 4 years ago #

    You could replace the_category with something like this:

    <?php if (in_category('ALL')) { ?>
       <a href="<?php bloginfo('url'); ?>" >ALL</a>
    <?php } else {
       the_category(', ');
    } ?>
  3. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    a bit intensive to get and compare the numbers of categories:
    post vs. blog:

    <?php
    $the_post_cats = wp_get_post_categories(get_the_ID());
    $all_the_cats = get_categories();
    if(count($the_post_cats)==count($all_the_cats)) { echo '<a href="'.get_bloginfo('url').'" >ALL</a>'; }
    else { the_category(', ');
    } ;
    ?>
  4. vtxyzzy
    Member
    Posted 4 years ago #

    Can you just assign the category 'ALL' as a second category to each of the general announcement posts and use the code I showed earlier? That way you won't need to change the code if you add new general announcement categories.

    Otherwise, you might be able to use something like this:

    <?php if (in_category('ga-1') && in_category('ga-2') && in_category('ga-3') && ... ) { ?>
       <a href="<?php bloginfo('url'); ?>" >ALL</a>
    <?php } else {
       the_category(', ');
    } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic