• I am creating an archive page. I am using the loop to display: post date and post title. I have posts that have between 2 to 3 categories assigned to them each at any given time. Though, I only have two kinds of posts, ‘articles’ and ‘links’ which are one of the categories assigned to each post.

    So, what I am looking to do is; display on the archive page: post date, post title and either ‘article’ or ‘links’. I’m guessing I need to query somehow whether or not each post has ‘article’ or ‘links’ assigned to it and then echo that result? I am just lost at how to do that.

    I should also note, I already have the date and title of each post working on the archive page. I am just looking to add the category display as well.

    Thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • In the loop, use the_category to display categories assigned to posts.

    Thread Starter ersnyder

    (@ersnyder)

    Hmmm, maybe I didn’t word that correctly; My posts have at least three categories each. However, on my archives page I only want to display one or two specific categories assigned to each post…

    I guess I would like to query the categories assigned to a post and if one of the category ID’s equal 7 or 45 then to display only those category names. I’m thinking this is getting me close:

    <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>

    …except I don’t know how to alter it to display one of the two categories I am looking for.

    Thread Starter ersnyder

    (@ersnyder)

    This also get’s me close, only displaying one category…

    <?php $cat = get_the_category(); $cat = $cat[0]; ?><?php echo $cat->cat_name; ?>

    …which is what I’m after, but it seems to display a random category rather than allowing me to target two specific categories no matter how I seem to manipulate it.

    <?php
    foreach((get_the_category()) as $category) {
      if ( $category->cat_ID == 7 || $category->cat_ID == 345 ) {
        echo $category->cat_name . ' ';
      }
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘One Category’ is closed to new replies.