我目前正在开发的一个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中的问题导致了问题。
试试这个
<?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; ?>