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 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题