Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用作为数组附加的所有图像时如何获取图像标题文本_Php_Wordpress - Fatal编程技术网

Php 调用作为数组附加的所有图像时如何获取图像标题文本

Php 调用作为数组附加的所有图像时如何获取图像标题文本,php,wordpress,Php,Wordpress,我试图在WordPress上获取存储在图像标题中的文本。我有一个代码,可以将图片附加到帖子上,再加上一段代码,我就可以在稍后的页面中显示它们 这就是将图像附加到帖子的原因 <?php if ($attachments = get_children(array( 'post_type' => 'attachment', 'post_mime_type' => 'image',

我试图在WordPress上获取存储在图像标题中的文本。我有一个代码,可以将图片附加到帖子上,再加上一段代码,我就可以在稍后的页面中显示它们

这就是将图像附加到帖子的原因

<?php
              if ($attachments = get_children(array(
                  'post_type' => 'attachment',
                  'post_mime_type' => 'image',
                  'numberposts' => -1,
                  'post_status' => null,
                  'post_parent' => $post->ID
                      )));

              foreach ($attachments as $attachment) {

                  $mynewarray = wp_get_attachment_image_src($attachment->ID, 'full');
                  $anotherarray [] = $mynewarray[0];
              } ?>

这将显示每个图像,我只是更改[2]以调用不同的图像

<?php echo $anotherarray[2]; ?>

所有的工作都很好,但我想把文本存储在标题中,我几个月前开始做这个,花了几天的时间试图得到标题,现在放弃了,回到这里。损失惨重,感谢任何帮助


谢谢你的建议,但还没有任何效果,我想这是因为我把贴在帖子上的所有图片都称为数组。然后使用数组部分将图像设置为div的背景。在这种情况下,文章中列出的作为笑脸问题的方法不起作用

每个附件在
posts
表中都有自己的行。该行的
post_摘录
列包含标题

因此,假设您知道附件的id,这将获得附件的标题

$attachment = get_post( $attachment_id );
$caption = $attachment->post_excerpt;
阅读本文了解更多细节,尤其是来自post_meta的ALT标记

编辑

您的代码中似乎有一个循环。你不能像这样抓取每个附件的post_摘录吗?除非出了什么大问题,否则这应该行得通

foreach ( $attachments as $attachment ) {
   $att = get_post( $attachment->ID );
   $caption = $att->post_excerpt;
   /*... */
   $mynewarray = wp_get_attachment_image_src($attachment->ID, 'full');
   $anotherarray [] = $mynewarray[0];
} 
你也可以试试。这个很好,因为它将处理字段中的内容,因此适合html显示

foreach ( $attachments as $attachment ) {
    $caption = get_post_field( 'post_excerpt', $attachment->ID );
     /* ... */
}

请查看此功能
wp\u为js准备附件($attachment\u id)
-

它将返回以下字段:

id
title
filename
url
link
alt
author
description
caption
name
status
uploadedTo
date
modified
menuOrder
mime
type
subtype
icon
dateFormatted
nonces
editLink
sizes
width
height
fileLength
compat

谢谢你的帮助,奥利,这没用,或者我应该说我没用。我正在使用代码的第一部分调用作为数组附加到帖子的所有图像,然后使用代码的第二部分设置一个div的背景图像(在本例中为3个div)。我想在div的内部显示标题文本。谢谢Ollie,我慢慢明白了你的意图。我已经尝试了你的建议,但还没有得到(100%是我的错)。你能用你的方法告诉我如何显示标题,比如我回显$anotherarray[0]来给我图像url吗。这个方法是否返回一个标题数组。