3

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?

4

4 回答 4

4

这似乎解决了这个问题:

$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' );
于 2013-10-28T14:22:44.450 回答
2

您需要将数组键用引号括起来,如下所示。所以这

$pic[id]

一定是

$pic['id']

或者

$pic["id"]
于 2013-10-28T14:13:22.857 回答
1

您需要用id单引号或双引号括起来:

$pic["id"]

或者:

$pic['id']

有关更多详细信息,请参阅使用方括号语法访问数组元素。

于 2013-10-28T14:13:29.693 回答
0

代替

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

 $imageurl = wp_get_attachment_image_src( $pic["id"], 'list-thumb' );
于 2013-10-28T14:13:50.290 回答