0

I'm trying to modify the output of a loop on an archive page for a specific list of categories. Normally this loop outputs the Title, Author, Date and does so for every post in that given category.

I have a list of categories in which I'd like to modify that output to include images. Up until now I've accomplished this by using if statements like below:

class new_loop extends theme_custom_loop {

 function modify_categories() {

  $cat1 = '101';
  $cat2 = '201';

  if (is_category($cat1)) {
    echo 'stuff';
  }

  elseif (is_category($cat2)) {
    echo 'stuff';
  }

  else theme_loop();

 }

}

Now I have a list of about 40 categories. I've tried to store them in an array and run them through a loop but it doesn't seem to work. Any advice? Current code is:

class new_loop extends theme_custom_loop {

  function modify_categories() {

    $archive_categories = array(
         '101',
         '201'
     );

     foreach($archive_categories as $archive_category) {
        if (is_category($archive_category)) {
            echo 'stuff';
        }

        else theme_loop();
     }
  }
}

1 Answer 1

1

You could use in_array() with get_queried_object_id():

if( is_category() && in_array( get_queried_object_id(), $archive_categories ) ){
    echo 'stuff';
} else {
    theme_loop();
}

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.