0

I'm using the Advanced Custom Fields Pro plugin and its Flexible Fields

I'm getting the following PHP warning for each layout related to the implode I'm using on the $displayCat variable:

Warning: implode() [function.implode]: Invalid arguments passed in /server-path/wp-content/themes/theme-name/page-home.php on line XX

I thought it was because $displayCat wasn't always an array, so tried to put $displayCat = array(); but that didn't eliminate the warnings.

Any ideas?

if( have_rows('home_content') ):

   // loop through the rows of data
   while ( have_rows('home_content') ) : the_row();

      // 1x1 Nav
      if( get_row_layout() == '1x1_nav' ):

      $img = get_sub_field('img');
      $alt = get_sub_field('alt');
      $url = get_sub_field('url');
      $displayCat = implode('" "', get_sub_field('display_cat'));
9
  • What are you trying to do? get_sub_field('display_cat') should return a single field... Commented Dec 2, 2014 at 13:36
  • By my understanding, get_sub_field() cannot return an array... You could only use while loops to iterate over the values... Commented Dec 2, 2014 at 13:36
  • Should be on Wordpress Developer Stack Exchange, not Stack Overflow. Commented Dec 2, 2014 at 13:36
  • 2
    @cybermonkey wordpress.stackexchange.com/questions/170032/… Commented Dec 2, 2014 at 14:06
  • 1
    @cybermonkey...As this has nothing to do with WordPress specifically, it doesn't belong on WPSE. It's a PHP question. Just because get_sub_field() is a function from a WordPress plugin function, it doesn't make this question invalid on SO. The error is a PHP error, caused by incorrect use of PHP functions. It has nothing to do with WordPress. Commented Dec 2, 2014 at 14:15

1 Answer 1

1

get_sub_field() doesn't return an array...it returns a string. Since you're trying to make an array out of individual strings, you should do the following:

$displayCat[] = get_sub_field('display_cat');

This will append each 'display_cat' sub-field to the $displayCat array.

Then, outside of your while() loop, you can implode(' ', $displayCat);

Sign up to request clarification or add additional context in comments.

4 Comments

Thought this would work, but didn't try to implement till this AM. I'm getting PHP Fatal error: [] operator not supported for string
Is $displayCat defined somewhere before the while() loop?
No, it's in the individual if statements of the while().
It has to have been initialized somewhere else, since PHP thinks it's a string. You should be able to put an additional $displayCat = array() right BEFORE the while() loop, to re-initialize it as an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.