dongwo5940 2014-10-25 16:02
浏览 46
已采纳

在wordpress中检索具有特定大小的自定义字段图像

I am using Custom MetaBoxes 2 to create an images uploader field.

Here is the code for the metabox:

$meta_boxes['home_page_slider'] = array(
        'id'           => 'home_page_slider',
        'title'        => __( 'Home Page Slider', 'cmb2' ),
        'object_types' => array( 'page', ), // Post type
        'context'      => 'normal',
        'priority'     => 'high',
        'show_names'   => true, // Show field names on the left
        'show_on'      => array( 'id' => array( 5, ) ), // Specific post IDs to display this metabox
        'fields'       => array(
            array(
                'name'         => __( 'Slider Images', 'cmb2' ),
                'desc'         => __( 'Upload or add multiple images/attachments.', 'cmb2' ),
                'id'           => $prefix . 'slider-images',
                'type'         => 'file_list',
                'preview_size' => array( 100, 100 ), // Default: array( 50, 50 )
            ),
        )
    );

How can I retrieve the images uploaded to this field, but with a specific size (for example "Full") , and display those images on the front-end on the following HTML markup?

<ul class="slides">
  <li>
    <img src="images/awn1.jpg" />
  </li>
  <li>
    <img src="images/awn2.jpg" />
  </li>
  <li>
    <img src="images/awn3.jpg" />
  </li>
</ul>
  • 写回答

2条回答 默认 最新

  • doukong1897 2014-10-26 14:33
    关注

    This code will do the trick:

    <ul class="slides">
    <?php
    
        $images = get_post_meta( 5, 'slider-images', true);
    
                if ( $images ) {
                  foreach ( $images as $attachment_id => $img_full_url ) {
    
                   $full = wp_get_attachment_link($attachment_id, 'full');
    
                    echo "<li>";
                    echo $full;
                    echo "</li>";
    
                  }
                }
    ?>
    </ul>
    
    • change the prefix with the same one of your repeatable group prefix;
    • '5' is the id of your post, or just leave it like 'get_the_ID();' to get the id automatic;
    • 'full' is the image size name, maybe at your side it is different as you can use the default wp thumbnail size which is 'thumbnail' or others sizes like 'full, medium, large'.

    Enjoy it and please rate my answer if you find it helpful.

    Thank you. Best, EF.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示