Wordpress get_post_meta延迟加载的图像宽度

时间:2013-04-24 07:04:32

标签: wordpress post get meta

我试图在Wordpress中懒得加载一页图片 - 我已经尝试了插件但是它们似乎都没有用(我只想在某些页面上懒得加载图片。)

所以我现在尝试使用插件 - http://www.appelsiini.net/projects/lazyload

此插件需要img标记中的图像宽度和高度。

    <img data-original=“img/example.jpg” src=“img/grey.gif” width=“640” height=“480”>

我正在附加自定义字段中的图片,例如

    <img src="<?php echo get_post_meta($post->ID, 'img1', true); ?>">

是否可以从get_post_meta获取图像的宽度和高度

我看过wp_get_attachment_image_src,但我看不到如何使用它get_post_meta

==

更新

==

    <?php
        $attachments = get_children( 
        array(
                    'post_parent' => $post->ID, 
            'post_status' => 'inherit', 
            'post_type' => 'attachment', 
            'post_mime_type' => 'image', 
            'order' => ASC, 
            'orderby' => 'menu_order ID'
            )
        );

    ?>

    <?php
        foreach ( $attachments as $attachment_id => $attachment ) {

            $image_attributes = wp_get_attachment_image_src( $attachment );

    ?>      
            <img src="<?php echo $image_attributes[0];?>" width="<?php echo $image_attributes[1];?>" height="<?php echo $image_attributes[2];?>">

    <?php       

        }
    ?>

1 个答案:

答案 0 :(得分:1)

最好使用Wordpress Media Gallery支持。您可以使用以下内容将所有图像附加到帖子中:

<?php $attachments = get_children( 
    array('post_parent' => $id, 
        'post_status' => 'inherit', 
        'post_type' => 'attachment', 
        'post_mime_type' => 'image', 
        'order' => ASC, 
        'orderby' => 
        'menu_order ID') ); ?>

其中$id是当前的帖子ID。然后使用附加图像的ID,您只需使用:

<?php foreach ( $attachments as $attachment_id => $attachment ) {
    (...)
}?>

迭代图像ID和

<?php wp_get_attachment_image_src($attachment_id, $size, $icon);>

描述:http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src,它不仅可以获取图像的网址,还可以获取其宽度和高度。

整个代码可以包含在短代码函数中以方便使用;)。