Jump to content

help with array


CyberShot

Recommended Posts

I am working in wordpress. There is code that will get the category id of a category using the slug name of that category. So I have a category called homepage. I need the id of that category which should be 1. This will get that id

 

$home = get_category_by_slug('homepage')->term_id;

 

This code does return 1.

 

What I wanted to do was modify it to take more parameters so that I could enter more than just one category and return them all. So I tried this

 

$home = array();
$home[] = get_category_by_slug(array('homepage','blog'))->term_id;

foreach($home as $key => $category){
   echo $key;
}

 

I get this error: Warning: preg_match() expects parameter 2 to be string, array given in....

Link to comment
https://forums.phpfreaks.com/topic/233248-help-with-array/
Share on other sites

Sorry mate, you can't do that. Refer to this page: http://codex.wordpress.org/Function_Reference/get_category_by_slug

It only takes a single string, so one slug, not multiple.

 

So re-arrange a bit of your code, and try it like this:

$slugs = array('homepage','blog');
foreach($slugs as $slug){
   $id = get_category_by_slug($slug)->term_id; // store the id of the slug
   echo $id;
}

Link to comment
https://forums.phpfreaks.com/topic/233248-help-with-array/#findComment-1199596
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.