简体   繁体   English

将后备图像添加到wordpress高级自定义字段循环

[英]Adding a fallback image to a wordpress advanced custom fields loop

I'm currently using the advanced custom fields plugin on a wordpress site that I'm developing and it is proving very useful. 我目前正在使用我正在开发的wordpress网站上的高级自定义字段插件,它证明非常有用。

I bought the repeater addon which had helped me generate a list of staff members. 我买了转发器插件,帮我生成一份工作人员名单。 However I imagine that there won't always be an image available for the staff member so I would like to use a fallback image or use an online placeholder image service like http://placehold.it ? 但是我想工作人员并不总是有可用的图像,所以我想使用后备图像或使用像http://placehold.it这样的在线占位符图像服务?

Here is the code: 这是代码:

                <?php if(get_field('about_the_practioner')): ?>

                <?php while(the_repeater_field('about_the_practioner')): ?>

                <article class="colleague_excerpts">

                         <figure>
                            <?php if(get_field('image')): ?>
                            <img src="<?php the_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
                            <?php else: ?>
                            <img src="http://placehold.it/160x160&text=Awaiting Image">
                            <?php endif; ?> 

                         </figure>

                        <div class="description">
                            <header>
                                <h4><?php the_sub_field('header')?><span><?php the_sub_field('title')?></span></h4>
                            </header>
                            <p><?php the_sub_field('text') ?></p>
                            <a href="<?php the_sub_field('page_link')?>" class="button"><?php the_sub_field('page_link_text') ?></a>
                        </div>
                    <hr>

                </article>

                <?php endwhile; ?>

                <?php endif; ?>

I've seen conditional statements used to create fallback images if a featured image is not available. 我已经看到如果特色图像不可用,用于创建后备图像的条件语句。 I have tried something similar in this case at the point where the image is called, I'm not good with php however and only the fallback image is brought into the webpage even if the image field is populated. 我在这种情况下尝试了类似的东西,在图像被调用的地方,我对PHP不好,但是即使填充了图像字段,也只有后备图像被带入网页。 Any help would be appreciated. 任何帮助,将不胜感激。

Just figured it out! 刚想通了! The functions that I was passing into the conditional statement were incorrect! 我传入条件语句的函数不正确! I was using the repeater field feature of advanced custom fields and should have been passing in get_sub_field for all the values above. 我正在使用高级自定义字段的转发器字段功能,并且应该已经在get_sub_field中传递上述所有值。 Its the little things that trip you up! 它是你绊倒的小事!

<?php if(get_sub_field('image')): ?>
<img src="<?php the_sub_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?>

Although I don't know what some of these functions return the function get_field('image') may return something no matter if there is an image or not, so instead of 虽然我不知道其中一些函数返回函数get_field('image')可能会返回一些内容,无论是否有图像,所以代替

<?php if(get_field('image')): ?>
<img src="<?php the_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?> 

Maybe try 也许试试吧

<?php if(!empty(the_field('image'))): ?>
<img src="<?php the_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?> 

Which returns the placeholder if the image is empty. 如果图像为空,则返回占位符。

I might be counting wrong, in fact I was. 我可能算错了,事实上我是。 Ignore this answer. 忽略这个答案。 I thought the brackets weren't matching up, and that a problem in the generated HTML was causing a problem. 我认为括号不匹配,并且生成的HTML中的问题导致了问题。 Jonny Beech has worked it out. Jonny Beech已经解决了这个问题。

Try this 尝试这个

 <?php 
$image = get_field('banner');
if( !empty($image) ): 
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
<?php else: ?>
<?php 

 echo "no image";?>


<?php endif; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM