0

I have a site with 5 different categories, each of them with a notices section. I would like also to have another section to see the latest notices from any category.

So far, I figured out that I can just use this in the URL ?cat=20,25,30,35 and it will return exactly what I want (if I'm not mistaken).

Now, I would like to know how could I create another template page for that particular section containing all the notices.

Usually I do something like this in my category.php:

if($this_category->slug == "groups-london" || $this_category->slug == "groups-cambridge"){
    include(TEMPLATEPATH.'/groups.php');
}else{
    include(TEMPLATEPATH.'/general.php');
}

BUt in this case I can not use the same approach because I noticed the $this_category only contains the information from the category passed as the first parameter in the URL. So, in this case ?cat=20,25,30,35 only from category with ID 20.

Any recommendation? Maybe I should just use a completely different approach to solve this issue?

  • What is $this_category? How is that set? – s_ha_dum May 26 '15 at 4:25
  • This $this_category = get_category($cat); – Alvaro May 31 '15 at 15:16
0

You need get_categories() with an include argument:

if (isset($_GET['cat'])) {
  $this_category = get_categories(
    array(
      'include' => $_GET['cat']
    )
  );
  var_dump($this_category);
}

You will get an array of stdClass objects containing category data. There are several ways to parse that but this might do:

if (isset($_GET['cat'])) {
  $this_category = get_categories(
    array(
      'include' => $_GET['cat']
    )
  );
  //   var_dump($this_category);
  $cats = wp_list_pluck($this_category,'slug');
  var_dump($cats);
  $needle = array('groups-london','groups-cambridge');
  var_dump(array_intersect($needle,$cats));
  if(array_intersect($needle,$cats)){
    include(TEMPLATEPATH.'/groups.php');
  }else{
    include(TEMPLATEPATH.'/general.php');
  }
}

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.