I currently am getting this error:

Warning: explode() expects parameter 2 to be string, object given in D:\...\portfolio.php on line 12

Here is my portofolio.php:

    $current = get_the_category(); 
    $current_id= $current[0] ->cat_ID; 
    $categs_list = get_category_parents($current_id);
    $pieces = explode("/", $categs_list);  //this is line 12.
    $category_name = strtolower($pieces[0]);
    $categs = get_cat_id($category_name);

Any Ideas? PHP is not my forte.

  • Could you clarify what it is that this code is even attempting to do? It's completely weird. – Otto Aug 24 '11 at 18:14

Let's start from the top:

  1. $current = get_the_category();

    This returns an array of objects, one object for each category.

  2. $current_id= $current[0] ->cat_ID;

    This returns an object, the first object in the previous array

  3. $categs_list = get_category_parents($current_id);

    This returns a string, but only if the current category has parents; otherwise, it returns whatever was passed to it: which was an object

    (See where the problem is yet?)

  4. $pieces = explode("/", $categs_list);

    This expects a string, but is returning an error, because it is being provided an object.

So, Conclusion:

You are calling this code in a Post that is in a category that doesn't have any parents. You need to write some fallback code to account for this occurrence.

| improve this answer | |
  • Thank you, I'm still getting the hang of it and having someone break the code apart the way you did is infinitely helpful. Thanks! (I'd give an upvote but not enough rep :( !) – Nick Aug 24 '11 at 20:57

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.