WordPress 图片附件

发布于 2024-11-16 12:22:04 字数 566 浏览 1 评论 0原文

我正在使用此代码来获取所有帖子附件,效果很好。 我的问题是我没有获得所需的附件大小。当我没有使用“the_attachment_link($attachment->ID);”指定大小时我得到缩略图,当我尝试添加“中、小、大或全”时,我只得到全尺寸。 我在这里错过了什么吗?我需要一些支持。

    $args = array(
        'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
); 
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
    echo apply_filters('the_title', $attachment->post_title);
    the_attachment_link($attachment->ID, 'medium', true);
}
}   

I am using this code to get all posts attachment which it is doing fine.
My problem is I am not getting the desired attachment size. When I don't specify the size with "the_attachment_link($attachment->ID);" I get the thumbnail and when I try by adding "medium, small, large, or full" I get the full size only.
Am I missing something here? I need some support please.

    $args = array(
        'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
); 
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
    echo apply_filters('the_title', $attachment->post_title);
    the_attachment_link($attachment->ID, 'medium', true);
}
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2024-11-23 12:22:04

您好,尝试这样的操作:

query_posts( array('post_type' => 'attachment','numberposts' => null,'post_status' => null,'post_parent' => $post->ID));
    <?php if (have_posts()) : while (have_posts()) :the_post(); 
            $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
    ?>
        <img src="<?php echo $src[0]; ?>" width="174px" height="142px" alt="" />                                
    <?php       
    endwhile; 
    else :          
        echo '<h3>Oops, something went wrong.</h3>';
    endif;
    wp_reset_query();
    ?>                      
    } ?>

使用 $src[0]。
根据您的喜好设置高度和宽度。

希望这能解决它。

Hi try something like this:

query_posts( array('post_type' => 'attachment','numberposts' => null,'post_status' => null,'post_parent' => $post->ID));
    <?php if (have_posts()) : while (have_posts()) :the_post(); 
            $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
    ?>
        <img src="<?php echo $src[0]; ?>" width="174px" height="142px" alt="" />                                
    <?php       
    endwhile; 
    else :          
        echo '<h3>Oops, something went wrong.</h3>';
    endif;
    wp_reset_query();
    ?>                      
    } ?>

make use of the $src[0].
set the height and width according to your preference.

hope this solves it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文