我一直在尋找這裏的問題/答案,但似乎無法找到適合我的代碼的東西。奇數/甚至在PHP循環中的html不正確交替
我發現的大多數解決方案都會導致整個頁面出現500錯誤。他們通常只是奇數/偶數的php片段,並不適合我輕鬆整合php自定義的post type循環。我可能只是沒有把它放在正確的地方,但似乎沒有任何工作。
我不是超級棒的php,但這是我一直有一個問題上班的一件事。
目標: 奇怪的職位有左頭像,生物信息在右邊。 即使帖子右側有爆頭,左側是生物信息。
以下是我的代碼,其中確實在頁面上加載了(沒有500錯誤),但不輸出交替佈局,只是輸出相同的佈局,就好像我沒有奇數/偶數代碼一樣。
<?php // theloop
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $loop = new WP_Query(array('post_type' => 'team', 'posts_per_page' => -1, 'order' => 'ASC')); ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<?php if ($wp_query->current_post % 2 == 0): ?>
<div class="row team-member"> <!--ODD LAYOUT // HEADSHOT LEFT - BIO RIGHT-->
<div class="container">
<div class="row is-table-row">
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if(get_field('additional_logo')): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if(get_field('phone_number')): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <a href="mailto:<?php the_field('email'); ?>" target="_top"><?php the_field('email'); ?></a></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php else: ?>
<div class="row team-member"> <!--EVEN LAYOUT // HEADSHOT RIGHT - BIO LEFT-->
<div class="container">
<div class="row is-table-row">
<div class="col-sm-6 bio cream-bg">
<div class="box">
<?php if(get_field('additional_logo')): ?>
<div class="additional-logo"><img src="<?php the_field('additional_logo'); ?>"></div>
<?php endif; ?>
<h2><?php the_field('name'); ?></h2>
<div class="bio-content"><?php the_field('bio'); ?></div>
<div class="contact-container">
<h4>Contact me!</h4>
<?php if(get_field('phone_number')): ?>
<p><i class="fa fa-phone" aria-hidden="true"></i> <?php the_field('phone_number'); ?></p>
<?php endif; ?>
<p><i class="fa fa-envelope" aria-hidden="true"></i> <a href="mailto:<?php the_field('email'); ?>" target="_top"><?php the_field('email'); ?></a></p>
</div>
</div>
</div>
<div class="col-sm-6 headshot" style="background-image:url(<?php the_field('bio_photo'); ?>);">
<div class="box">
</div>
</div>
</div>
</div>
</div>
<?php endif ?>
<?php endwhile; wp_reset_query(); ?>
有什麼我做錯了這裏,或較好地解決了呢?我很沮喪,理論上這是一個簡單的請求,我似乎無法正常工作。任何幫助深表感謝!
什麼定義了左右佈局? –