4

I have defined two variables as follows:

$pic = get_tax_meta($books->term_id,'books_field_id', true);
$imageurl = wp_get_attachment_image_src( $pic[id], 'list-thumb' );

print_r($pic) results in the following:

Array ( [id] => 302 [src] => http://localhost/mysite/wp-content/uploads/2013/10/apic.jpg )

However, I get the following warning from $pic[id]:

Warning: Illegal string offset 'id'

Any idea what I'm doing wrong?

user1444027
  • 4,983
  • 7
  • 29
  • 39

4 Answers4

4

This seems to have fixed the problem:

$pic = get_tax_meta($books->term_id,'books_field_id', true);
if (isset($pic['id'])) {
      $picid = $pic['id'];
};
$imageurl = wp_get_attachment_image_src( $picid, 'list-thumb' );
user1444027
  • 4,983
  • 7
  • 29
  • 39
2

You need to wrap the array key in quotes like the following.So This

$pic[id]

Must be

$pic['id']

Or

$pic["id"]
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
1

You need to wrap id in either single or double quotes:

$pic["id"]

Or:

$pic['id']

See Accessing array elements with square bracket syntax for more detail.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

Replace

 $imageurl = wp_get_attachment_image_src( $pic[id], 'list-thumb' );

with

 $imageurl = wp_get_attachment_image_src( $pic["id"], 'list-thumb' );
opalenzuela
  • 3,139
  • 21
  • 41