首页 > 解决方案 > 如何在 Wordpress 中使用简码显示子字段(图像)?

问题描述

这些是自定义字段组成:

1 组页脚

1.1 组 Col2

1.1.1 bbcimg(这是图片ID)

1.1.2 bbc-rss(这是 RSS 提要 ID)

我有以下代码显示 bbc-rss:

<div class="col-md-3">
               <?php
                $footer = get_field('footer'); // 'footer' is your parent group
                $col2 = $footer['col2']; // 'col2' is your child group
                ?>
                <div class="widget_item widget_latest sm-m-top-50">
                    <h4 id="white" class="text-white">Latest News</h4>
                    <div class="widget_latst_item m-top-30">
                        
                        <div class="item_icon"><img src="<?php bloginfo('template_directory');?>/img/rss/bbc_rss.png" alt="" /></div>
                        <div id="gris" class="widget_latst_item_text">
                            <p><?php echo $col2['bbc-rss'];?></p>

                        </div>
                    </div>
                    <div class="widget_latst_item m-top-30">
                        <div class="item_icon"><img src="<?php bloginfo('template_directory');?>/img/rss/reuters_rss.png" alt="" /></div>
                        <div id="gris" class="widget_latst_item_text">
                            <p><?php echo $col2['reuters-rss'];?></p>

                        </div>
                    </div>
                    <div class="widget_latst_item m-top-30">
                        <div class="item_icon"><img src="<?php bloginfo('template_directory');?>/img/rss/cnbc.jpg" alt="" /></div>
                        <div id="gris" class="widget_latst_item_text">
                            <p><?php echo $col2['cnbc-rss'];?></p>

                        </div>
                    </div>
                </div><!-- End off widget item -->
            </div><!-- End off col-md-3 -->

我创建了具有以下属性的图像字段 bbcimg:

并且图像已经上传到自定义字段中。见图片: 在此处输入图像描述

问题:

如何编写在网站上显示图片的逻辑?

非常感谢您!

一直在尝试使用

标签: phpwordpressshortcodecustom-fields

解决方案


首先,您需要找出自定义字段中返回的内容。print_r在某处添加一个好的旧代码:

<?php print_r($col2['bbcimg']) ?>

这将打印出一个值数组,例如 Array('url' => 'http....something.jpg', 'size'=>'full', ..etc)

然后,您可以确定哪个数组值为您提供了图像的完整 URL。假设在这种情况下它将是一个名为 的字段url,那么您可以按如下方式使用该值:

<div id="gris" class="widget_latst_item_text">
     <img src="<?php echo $col2['bbcimg']['url']; ?>" alt=""/>
     <p><?php echo $col2['bbc-rss'];?></p>
</div>

推荐阅读