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

问题描述:

我目前正在使用我正在开发的wordpress网站上的高级自定义字段插件,它证明非常有用。将一个后备图像添加到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_field(“图像”)可以返回的东西,不管是否有图像或没有,所以不是

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

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

+0

为你的思想@levigideon非常感谢。我正在考虑以类似的方式使用isset?我尝试了你的想法,并得到了一个致命的错误 致命错误:无法在/ Users/jonbeech/Sites/lancscps第二个站点/ lancscps.dev/wp-content/themes/lancscps主题/ page- aboutus.php 28行 – 2013-03-16 10:47:39

+0

只是想通了!我传递给条件语句的函数不正确!我用先进的自定义字段的中继器场特征和应在get_sub_field已经通过为所有的值以上 'code' \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t 'code' – 2013-03-16 10:58:48

只是想通了!我传递给条件语句的函数不正确!我正在使用高级自定义字段的中继器字段功能,并且应该已经在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; ?> 

我可能算错了,其实是我。忽略这个答案。我认为括号不匹配,并且生成的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; ?>