Php 向wordpress高级自定义字段循环添加回退映像
我目前正在开发的wordpress网站上使用高级自定义字段插件,它非常有用 我买了repeater插件,它帮助我生成了一份员工名单。然而,我想不会总是有一个图像可供工作人员使用,所以我想使用一个备用图像或使用一个在线占位符图像服务,如 代码如下:Php 向wordpress高级自定义字段循环添加回退映像,php,Php,我目前正在开发的wordpress网站上使用高级自定义字段插件,它非常有用 我买了repeater插件,它帮助我生成了一份员工名单。然而,我想不会总是有一个图像可供工作人员使用,所以我想使用一个备用图像或使用一个在线占位符图像服务,如 代码如下: <?php if(get_field('about_the_practioner')): ?> <?php while(the_repeater_field('abou
<?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_字段(“图像”)可能返回一些内容,无论是否有图像,因此
<?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; ?>
“alt=”“>
也许试试
<?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; ?>
“alt=”“>
如果图像为空,则返回占位符。虽然我不知道其中一些函数返回什么,但函数get_字段('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; ?>
“alt=”“>
也许试试
<?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; ?>
“alt=”“>
如果图像为空,则返回占位符。刚刚找到它!我传递到条件语句中的函数不正确!我使用的是高级自定义字段的repeater字段功能,应该为上面的所有值传入get_sub_字段。是那些小事把你绊倒了
<?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; ?>
“alt=”“>
刚刚弄明白了!我传递到条件语句中的函数不正确!我使用了高级自定义字段的repeater字段功能,应该为上面的所有值传递get\u sub\u字段。这些小事情让你大吃一惊
<?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; ?>
“alt=”“>
我可能数错了,事实上我是。忽略这个答案。我认为括号不匹配,生成的HTML中的问题导致了问题。琼尼·比奇已经算出来了。我可能数错了,事实上我是。忽略这个答案。我认为括号不匹配,生成的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; ?>
“alt=”“/>
试试这个
<?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; ?>
“alt=”“/>
非常感谢您的想法@levigideon。我正在考虑以类似的方式使用isset?我尝试了你的想法,但遇到了一个致命错误致命错误:无法在第28行的/Users/jonbeech/Sites/lancscps 2nd site/lancscps.dev/wp-content/themes/lancscps-theme/page-aboutus.php中的写上下文中使用函数返回值刚刚解决了这个问题!我传递到条件语句中的函数不正确!我使用的是高级自定义字段的repeater字段功能,应该为上面的所有值传入get_sub_字段code
“alt=”“>code
非常感谢您的想法@levigideon。我正在考虑以类似的方式使用isset?我尝试了你的想法,但遇到了一个致命错误致命错误:无法在第28行的/Users/jonbeech/Sites/lancscps 2nd site/lancscps.dev/wp-content/themes/lancscps-theme/page-aboutus.php中的写上下文中使用函数返回值刚刚解决了这个问题!我传递到条件语句中的函数不正确!我使用的是高级自定义字段的repeater字段功能,应该为上面的所有值传入get_sub_字段code
“alt=”“>code