So I'm trying this code:

global $post;
$my_query = new WP_Query('showposts=8&category_name=featured');

while ($my_query->have_posts()):
    $do_not_duplicate = $post->ID;
    $category = get_the_category();
    $content = get_the_content();
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID) , 'main-thumbs');
    $tn_id = get_post_thumbnail_id($post->ID);
    $img = wp_get_attachment_image_src($tn_id, 'main-thumbs');
    $width = $img[1];
    $height = $img[2];
<div class="cat-square <?php
    echo esc_html($category[0]->slug) ?>">

with this, I can style a generic div (cat-square) and then add some additional styling depending on the category the post is in. So far so good, this work as a charm. Exception made that categories, which are all scientific disciplines, can also belong to Featured category . So, sometimes the category (let's say Physics) is replaced by Featured category.

How can prevent this from happening? Please note that I don't mean to exclude the post itself, only the Featured category nicename and slug

  • Have you tried the category__not_in parameter? I haven't tried using category_name and category__not_in at the same time, but it may work.
    – coopersita
    Jun 2 '16 at 22:50
  • yes, but it excludes the post as well Jun 2 '16 at 22:58

The function get_the_category is returning you an array of objects (see codex for more info), so if the post has more than one term assigned, then you are just outputting the first one.

So I think that doing a foreach loop will solve your problem:

foreach ($category as $cat) {
    if ($cat->slug != 'featured') {
        echo esc_html($cat->slug);

Hope it helps!

  • Capiedge, I can't mark your question as correct because the site doesn't allow me to complete the registration, but I want to thank you and confirm this is the correct solution indeed! (btw, it's me, Ariel, just that this site is the anti-UX site and does everything a site is not supposed to do) Jun 3 '16 at 16:36
  • well, I could do it with another account, lol :D Jun 3 '16 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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