[英]Conditional PHP if and else statements within existing accordion
我在Wordpress中使用Advanced Custom Fields插件,並且已經設法(使用非常有限的PHP知識)使用其轉發器字段來創建手風琴。
除了圖片字段(company_logo),其他所有東西都運行良好。 只要用戶為該自定義字段選擇圖像,它就會顯示正常,但是如果他們不選擇圖像,則會顯示一些奇怪的文字。
使用我當前的代碼,我試圖添加一個'if'語句,這樣,如果他們不選擇圖像,它將顯示默認圖像。 我已經做了很多改進,但是無法正常工作。
任何人都可以幫助/指出我正確的方向嗎? 另外,如果有辦法可以清理的話,因為我似乎經常使用
<div id="accordion">
<?php if( have_rows('exhibitor') ): ?>
<?php while( have_rows('exhibitor') ): the_row(); ?>
<h4 class="accordion-toggle"><?php the_sub_field('exhibitor_type'); ?></h4>
<div class="accordion-content">
<?php while( have_rows('table') ): the_row(); ?>
<div class="exhibitor-single">
<p class="table-field">Table <?php the_sub_field('table_no'); ?></p>
<p><?php the_sub_field('company_name'); ?></p>
<?php $image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<p><a href="http://<?php the_sub_field('company_website'); ?>" target="blank"><?php the_sub_field('company_website'); ?></a></p>
</div>
<?php endwhile; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
您應該能夠在其中包裝圖像“ section”:
<?php
if(get_sub_field('company_logo')) :
$image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo');
?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<?php
else :
?>
<!-- Your default image here -->
<img src="" alt="" />
<?php
endif; ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.