Wordpress get_post_meta 图像宽度用于延迟加载

问题描述 投票:0回答:1

我正在尝试在 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       

        }
    ?>
wordpress
1个回答
2
投票

最好使用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,它不仅可以获取图像的url,还可以获取其宽度和高度。

整个代码可以封装在一个短代码函数中以方便使用;)。

© www.soinside.com 2019 - 2024. All rights reserved.