【问题标题】:Display all images of a post with a specific thumbnail size显示具有特定缩略图大小的帖子的所有图像
【发布时间】:2012-12-15 10:41:09
【问题描述】:

我正在使用这个 sn-p 来显示帖子的所有图像:

<?php
$argsThumb = array(
    'order'          => 'ASC',
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_mime_type' => 'image',
    'post_status'    => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
    foreach ($attachments as $attachment) {
        //echo apply_filters('the_title', $attachment->post_title);
                echo '<img src="'.wp_get_attachment_url($attachment->ID, 'testsize', false, false).'" />';
    }
}
?>

此代码用于创建自定义缩略图大小

add_image_size( 'testsize', 400, 400, true );

不幸的是,它没有输出 400px X 400px 的图像,尺寸只是原始尺寸。 (注意:我重新生成了缩略图并在帖子中添加了新图片,但仍然没有用。

【问题讨论】:

    标签: wordpress image post image-size


    【解决方案1】:

    答案如下: wp_get_attachment_url() 不接受参数。 使用 wp_get_attachment_image() 代替工作。

    <?php
    $argsThumb = array(
        'order'          => 'ASC',
        'post_type'      => 'attachment',
        'post_parent'    => $post->ID,
        'post_mime_type' => 'image',
        'post_status'    => null
    );
    $attachments = get_posts($argsThumb);
    if ($attachments) {
        foreach ($attachments as $attachment) {
            //echo apply_filters('the_title', $attachment->post_title);
            echo '<img src="'.wp_get_attachment_image($attachment->ID, 'testsize').'" />';
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2015-05-24
      • 2016-11-21
      • 1970-01-01
      • 2011-10-26
      相关资源
      最近更新 更多