【问题标题】:Adding a fallback image to a wordpress advanced custom fields loop将后备图像添加到 wordpress 高级自定义字段循环
【发布时间】:2018-01-23 09:08:30
【问题描述】:

我目前在我正在开发的 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,即使填充了图像字段,也只会将后备图像带入网页。任何帮助将不胜感激。

【问题讨论】:

    标签: php


    【解决方案1】:

    刚刚想通了!我传递给条件语句的函数不正确!我正在使用高级自定义字段的转发器字段功能,并且应该为上述所有值传递 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; ?>
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 非常感谢您的想法@levigideon。我正在考虑以类似的方式使用isset?我尝试了你的想法并得到了一个致命错误致命错误:无法在 /Users/jonbeech/Sites/lancscps 2nd site/lancscps.dev/wp-content/themes/lancscps theme/page-aboutus 的写入上下文中使用函数返回值.php 在第 28 行
      • 刚刚想通了!我传递给条件语句的函数不正确!我正在使用高级自定义字段的转发器字段功能,并且应该为上面的所有值传递 get_sub_field code " alt=""> placehold.it/160x160&text=AwaitingImage"> code
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        试试这个

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

        【讨论】:

          猜你喜欢
          • 2017-01-14
          • 2013-10-06
          • 2016-09-23
          • 2018-02-24
          • 2016-08-12
          • 2018-11-24
          • 2023-03-03
          • 2017-01-12
          • 2019-11-07
          相关资源
          最近更新 更多