Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.