CyberShot Posted April 10, 2011 Share Posted April 10, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/233248-help-with-array/ Share on other sites More sharing options...
JasonLewis Posted April 10, 2011 Share Posted April 10, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/233248-help-with-array/#findComment-1199596 Share on other sites More sharing options...
CyberShot Posted April 10, 2011 Author Share Posted April 10, 2011 That worked, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/233248-help-with-array/#findComment-1199694 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.