[英]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.