繁体   English   中英

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

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

我目前正在使用我正在开发的wordpress网站上的高级自定义字段插件,它证明非常有用。

我买了转发器插件,帮我生成一份工作人员名单。 但是我想工作人员并不总是有可用的图像,所以我想使用后备图像或使用像http://placehold.it这样的在线占位符图像服务?

这是代码:

                <?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; ?>

我已经看到如果特色图像不可用,用于创建后备图像的条件语句。 我在这种情况下尝试了类似的东西,在图像被调用的地方,我对PHP不好,但是即使填充了图像字段,也只有后备图像被带入网页。 任何帮助,将不胜感激。

刚想通了! 我传入条件语句的函数不正确! 我正在使用高级自定义字段的转发器字段功能,并且应该已经在get_sub_field中传递上述所有值。 它是你绊倒的小事!

<?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; ?>

虽然我不知道其中一些函数返回函数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; ?> 

也许试试吧

<?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; ?> 

如果图像为空,则返回占位符。

我可能算错了,事实上我是。 忽略这个答案。 我认为括号不匹配,并且生成的HTML中的问题导致了问题。 Jonny Beech已经解决了这个问题。

尝试这个

 <?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