0

What I want to accomplish

I need an aside to pull the last 5 posts from a specific set of 5 categories.

Not the latest of each, not the last 5 of each, but the last 5 total posts from the 5 categories.

The category IDs are: 89,90,91,92,93

Background

I have 5 different category pages, so this has been making it trickier. Each of these uses an include for the aside. I am writing that logic in that include.

PHP

<?php 
    if ( in_category( 92, $post_id ) ) : ?>
    $bfaCat = 'bfa-cat-ba';

<?php endif; ?>

Then I have to loop through. Not applying anything if the post is not in any of the categories. But if the posts are of that category then add a variable (as $bfaCat) with the following class (on right) applied if it has the following category id (on left)

89 - bfa-cat-bl
90 - bfa-cat-fs
91 - bfa-cat-s1
92 - bfa-cat-ba
93 - bfa-cat-ea

HTML - ATTEMPT 1 - in template

<div class="bfa-category <?=$bfaCat;?>">
    <?php the_category(); ?>
</div>

HTML - desired output example

<div class="bfa-category bfa-cat-bl">
    <?php the_category(); ?>
</div>

HTML - ATTEMPT 2 - Would this be better if modified?

I know this is sloppy but I also tried it inline but it just applied this class to everything

<div class="bfa-category <?php if (get_category('91')) echo 'bfa-cat-s1'; ?>">
    <?php the_category(); ?>
</div>

So it seems like the class is being echoed into everything.

3
  • 1
    What happens if you replace get_gategory( '91' ) with in_category( 91 ) or in_category( 91, ... post id ... ) within your WP_Query loop in attempt #2?
    – birgire
    Commented Jun 23, 2017 at 8:40
  • @birgire I read this after finding a solution, would you suggest that I do it differently? What you wrote made more sense than my initial thoughts on the second solution i tried.
    – JGallardo
    Commented Jun 23, 2017 at 21:23
  • You might simplify it by using an array. If bl, fs, ... are category slugs, I wonder if you can just prefix the current category slug(s) with bfa-cat- and skip the if check part.
    – birgire
    Commented Jun 24, 2017 at 8:55

2 Answers 2

1

get_category('91') is always going to return an object of category ID 91's data. This means your if statement is always going to return as true. You need to get the current iterated posts assigned category ID and then check if it is 91.

<!-- Return objects of current iterated posts associated categories -->
$categories = get_the_category();
<!-- Get current iterated posts assigned category ID from $categories object. The [0] is because a post can be assigned to multiple categories. 
In your example you are probably only assigning one category per post, so you just need to grab the ID of the first category in the returned object. -->
$category_id = $categories[0]->cat_ID;
<!-- Check if current iterated assigned posts category ID is equal to 91 -->
<div class="bfa-category <?php if ($category_id == 91) echo 'bfa-cat-s1'; ?>">
    <?php the_category(); ?>
</div>
2
  • Thank you for the clarification. Ok so based on that I finished the loop for my solution and it works in my project.
    – JGallardo
    Commented Jun 23, 2017 at 21:21
  • Excellent, glad I could help Commented Jun 23, 2017 at 22:15
0

This is what I ended up coding based on help. I used the PHP suggested to get assign the category. cat_ID; ?>

Then below it I put that above the actual element where I created the logic to assign the appropriate class

PHP

<div class="bfa-category 
    <?php 
        if ($category_id == 89) {
            echo 'bfa-cat-bl';
        } elseif ($category_id == 90) {
            echo 'bfa-cat-fs';
        } elseif ($category_id == 91) {
            echo 'bfa-cat-s1';
        } elseif ($category_id == 92) {
            echo 'bfa-cat-ba';
        } elseif ($category_id == 93) {
            echo 'bfa-cat-ea';
        }
    ?>">
    <?php the_category(); ?>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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