WordPress.org

Ready to get started?Download WordPress

Forums

is_category / in_category -> echo value from array (3 posts)

  1. Julson
    Member
    Posted 2 years ago #

    Hi,

    I'm trying to echo a value from an array with several names of categories.
    The problem is that I can't figure out how to echo the specific value (=the category) rather than the word "Array".
    Could you help me? Thanks!

    function cat_background_image (){
    
    $category_class = array('wissen', 'meinung', 'wirtschaft'); //category_names
    
    if ( is_single() ) {
    
    if(in_category($category_class))  echo  'class="bg_' .$category_class. '" ';
    
    } else {
    
    if(is_category($category_class))  echo  'class="bg_' .$category_class. '" ';
    
    }
    }
  2. vtxyzzy
    Member
    Posted 2 years ago #

    I think this is what you want (UNTESTED). Replace this:

    if(in_category($category_class))  echo  'class="bg_' .$category_class. '" ';

    with this:

    foreach ($category_class as $cat_class ) {
       if (in_category($cat_class)) {
          echo  'class="bg_' .$cat_class. '" ';
          break;
       }
    }

    Use a similar replacement for the is_category() test.

  3. Julson
    Member
    Posted 2 years ago #

    Oh yeah, it works ;) And again it's the loop! :D Thanks a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic