特色图片的标题和标题未在WordPress上的图像元显示中显示

时间:2015-01-18 11:54:55

标签: wordpress

我已将特色图片添加到WordPress页面,并且我已经为图像添加了标题和标题。

然后我使用以下代码访问图像:

<?php if (has_post_thumbnail( $post->ID ) ): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
  <?php $meta = wp_get_attachment_metadata( get_post_thumbnail_id( $post->ID), 'single-post-thumbnail' ); ?>

  <?php print_r($meta); ?>
    <div class="full_page_photo" style="background-image: url('<?php echo $image[0]; ?>');">
<?php endif; ?>

<?php print_r($meta); ?>标记仅用于测试,测试已返回该变量中包含的以下信息:

  Array
(
    [width] => 1920
    [height] => 480
    [file] => 2015/01/01.png
    [sizes] => Array
        (
            [thumbnail] => Array
                (
                    [file] => 01-150x150.png
                    [width] => 150
                    [height] => 150
                    [mime-type] => image/png
                )

            [medium] => Array
                (
                    [file] => 01-300x75.png
                    [width] => 300
                    [height] => 75
                    [mime-type] => image/png
                )

            [large] => Array
                (
                    [file] => 01-1024x256.png
                    [width] => 1024
                    [height] => 256
                    [mime-type] => image/png
                )

            [post-thumbnail] => Array
                (
                    [file] => 01-624x156.png
                    [width] => 624
                    [height] => 156
                    [mime-type] => image/png
                )

        )

    [image_meta] => Array
        (
            [aperture] => 0
            [credit] => 
            [camera] => 
            [caption] => 
            [created_timestamp] => 0
            [copyright] => 
            [focal_length] => 0
            [iso] => 0
            [shutter_speed] => 0
            [title] => 
            [orientation] => 0
        )

)

出于某种原因,即使我为图像提供了标题和标题,这些字段也会返回为空白。在编辑页面时,图像显示我提供给它的标题和标题,因此信息将保存到系统中。

为什么返回空白字段?

2 个答案:

答案 0 :(得分:1)

我也在寻找这些信息。我在Wordpress: Get description, caption, alt, file URL, from an image ID without the image being a post thumbnail

中找到了答案
  

尝试wp_prepare_attachment_for_js(http://codex.wordpress.org/Function_Reference/wp_prepare_attachment_for_js

     

我还没有使用它,但我知道它应该从&gt;附件中获取所有信息,包括标题,描述,替代,标题等。

该函数返回各种有用信息的数组。

答案 1 :(得分:1)

我遇到了标题和alt没有显示的相同问题。经过一番搜索,我找到了一个可用的功能来显示图像的数据。所以如果还有其他人仍在这里,那就是。

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}