【问题标题】:Odd/even html in php loop not alternating properlyphp循环中的奇数/偶数html没有正确交替
【发布时间】:2017-12-11 23:21:25
【问题描述】:

我一直在这里搜索问题/答案,但似乎找不到适用于我的代码的内容。

我发现的大多数解决方案都会导致整个页面出现 500 错误。它们通常只是奇数/偶数 php 的 sn-ps,并且不适用于我轻松与我拥有的自定义帖子类型循环 php 集成。我可能只是没有把它放在正确的地方,但似乎没有任何效果。

我不太擅长 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(); ?> 

我在这里做错了什么,或者有更好的解决方案吗?我很沮丧,理论上这是一个简单的请求,我似乎无法正常工作。非常感谢任何帮助!

【问题讨论】:

  • 什么定义了左右布局?

标签: php wordpress loops count custom-post-type


【解决方案1】:

好的,专业提示:程序员很懒惰。我们喜欢DRY 原则。我们不喜欢重复代码,也不喜欢维护巨大的重复代码块。

因此,下面是您的循环的修改版本,它更简单,重复更少。我鼓励您考虑其他减少重复的方法,例如,使用 CSS 类(可能是浮点数)来交替在左侧或右侧,并且一次只渲染一个版本的 HTML。

具体问题是您没有访问正确查询对象的$current_post 属性。您应该使用$loop-&gt;current_post 而不是$wpdb-&gt;current_post。但是,为了超级清晰/明确,我会手动设置一个计数器,然后改用它:

<?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 
        // initialize the counter here
        $post_count = 0;
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <div class="row team-member">
            <div class="container">
                <div class="row is-table-row">
                   <?php 
                    // move the if condition here to reduce / simplify code
                    // reference (and increment) the counter
                    if ($post_count++ % 2 == 0): ?>
                    <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>
                 <?php else: ?>
                    <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>
                   <?php endif; ?>     
                </div>
            </div>
        </div>    
<?php endwhile; wp_reset_query(); ?> 

【讨论】:

  • 太棒了,效果很好!我也感谢您的反馈:)
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 2021-11-04
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多