【问题标题】:Simple Wordpress (PHP) Array access. How to get a value out of an array简单的 Wordpress (PHP) 数组访问。如何从数组中取出一个值
【发布时间】:2013-01-03 16:25:00
【问题描述】:

我是 PHP 新手,正在尝试编写一个 Wordpress 主题。这是我第一次在PHP中遇到数组并且不知道如何使用它们......

我有以下代码行:

photo url: <?php echo get_post_meta($post->ID, 'member-photo', true);?>

我使用自定义字段成员照片而不是显示我上传到 wordpress 记录中的文件的 url 只显示“数组”。

查看http://ebf.websiteinprogress.co.uk/members/scott-griffiths/就可以看到这个

我现在知道我需要的 URL 存储在一个数组中,但不知道如何修改我的代码来访问这个值。

我真正想要实现的是使用这个值来渲染图像,所以我需要的最终代码会这样做:

<?php if(get_post_meta($post->ID, 'member-photo', true)):?><p><span itemprop="photo"><img src="***URL-TO-GO-HERE***" alt="<?php the_title()?>" title="<?php the_title()?>" width="200"></span></p><?php endif;?>

非常感谢您的帮助。 我需要将代码添加到页面以使其工作,而不是解释如何做到这一点,因为恐怕我在 PHP 方面没有那么有才华。我是一名 SEO,试图让某些事情发挥作用!

【问题讨论】:

  • 你试过print_r(get_post_meta($post-&gt;I[...]) 吗?这样您就可以看到数组中的内容。
  • echo get_post_meta($post-&gt;ID, 'member-photo', true); 应该是一个字符串,所以让我觉得其他地方有问题。确保$post-&gt;IDmember-photo 是正确的。例如print_r($post-&gt;ID) 并仔细检查您是否拥有正确的元密钥。

标签: php arrays wordpress


【解决方案1】:

get_post_meta 总是返回一个数组。您想要的项目是第一个元素,所以您所要做的就是: 照片 url: ID, 'member-photo', true)[0];?>

查看数组以进行调试的更好方法是 var_dump(variable);

【讨论】:

  • 根据文档,get_post_meta($post-&gt;ID, 'member-photo', true) 应该返回一个字符串。 If $single is set to true, the function returns the first value of the specified key (not in an array) get_post_meta
猜你喜欢
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2015-02-28
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多