首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在最后一篇文章和每5篇文章中添加div

如何在最后一篇文章和每5篇文章中添加div
EN

Stack Overflow用户
提问于 2019-10-14 22:49:12
回答 1查看 49关注 0票数 0

循环它目前被设置为每5个项目帖子添加一个‘评论’,我希望它这样做,并在最后一个帖子之后添加一个‘评论’。例如,如果有13个帖子,它会在第5、10和13个帖子后面添加一个“评论”。

我试着做了一个帖子计数,并将其添加为一个'or‘语句,然而,它只是试图在每个帖子后面添加一个’评论‘。

代码语言:javascript
复制
<?php
/**
 * Template Name: Work
 *
 * This template display content at full with, with no sidebars.
 * Please note that this is the WordPress construct of pages and that other 'pages' on your WordPress site will use a different template.
 *
 * @package some_like_it_neat
 */

get_header(); ?>


<div class="content-area">
    <section class="work not-home work-content page-content ">
        <div class="row page-header">
            <h1><?php echo get_field('page_title', get_the_ID()); ?></h1>
            <div class="work-page-content">
                <?php echo get_field('page_content', get_the_ID()); ?>
            </div>
        </div>

        <div class="project-section">
            <div class="row">
                <?php 
                $posts = get_posts();
                $count = count($posts); 

                $args = array(
                    'post_type' => 'work',
                    'posts_per_page' => -1
                );
                $q = new WP_Query( $args );

                $posts_iterator = 0; //set to last to display first icon
                $icon_iterator = 0;
                $is_right = true;
                $review_iterator = 0;



                $icons = get_field('icons', get_the_ID());
                $reviews = get_field('reviews', get_the_ID());

                $true_reviews = get_sub_field('rave_review', get_the_ID());

                ?>

                <?php if ( $q->have_posts() ): ?>
                <div class="works">
                    <?php while ( $q->have_posts() ): $q->the_post(); ?>
                        <div class="featured-work">
                            <?php if($posts_iterator == 0): ?>
                                <div style="text-align: <?php echo $is_right ? 'right' : 'left' ?>;">
                                    <img class="work-icon" src="<?php echo $icons[$icon_iterator]['icon_image'] ?>" />
                                </div>
                            <?php 
                                $icon_iterator++;
                                if ( $is_right ) {
                                    $is_right = false;
                                } else {
                                    $is_right = true;
                                }
                                if( $icon_iterator == count($icons)) {
                                    $icon_iterator = 0;
                                }

                                endif; 
                            ?>
                            <a class="lazy featured-work__link arrow-hover" href="<?php the_permalink(); ?>">
                                <div class="project-panel">
                                    <div class="featured-work-img">
                                        <img src="<?php the_field('home_image'); ?>">
                                    </div>
                                    <div class="featured-work-spacer">
                                        &nbsp;
                                    </div>
                                    <div class="featured-work-text match">
                                        <div class="featured-work-text-inner">
                                            <p class="categories"><?php the_field('category'); ?></p>
                                            <h3 class="project-name link-arrow"><?php the_title(); ?></h3>
                                        </div>
                                    </div>
                                </div>

                            </a>
                        </div>




                        <?php if($posts_iterator == 4 && $review_iterator != count($reviews) ) : ?>
                            <div class="rave-review work-content__text-wrap">
                                <div class="review-inner">
                                    <p><?php echo $reviews[$review_iterator]['reviewer_copy'] ?></p>
                                </div>
                                <div class="reviewer-info">
                                    <p>
                                        <span class="reviewer-name"><?php echo $reviews[$review_iterator]['reviewer_name'] ?></span>
                                        <span class="reviewer-title"><?php echo $reviews[$review_iterator]['reviewer_title'] ?></span>
                                        <span class="reviewer-company"><?php echo $reviews[$review_iterator]['reviewer_company'] ?></span>
                                    </p>
                                </div>
                            </div>
                        <?php 
                            $review_iterator++;
                            endif;
                        ?>



                    <?php
                        if( $posts_iterator == 4 ) {
                            $posts_iterator = 0;

                        } else {
                            $posts_iterator++; 

                        }
                        endwhile;
                    ?>

                </div>
                <?php endif; ?>
            </div>
        </div>
    </section>


</div><!-- #primary -->

<?php get_footer(); ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-14 23:25:21

有许多方法可以实现这一点,但基本上您希望在循环的末尾使用modulo,以避免重复。

假设您有一个带有计数器的循环,该循环执行10次、15次或20次迭代。在循环结束时(在循环之外),您希望检查该计数器:

代码语言:javascript
复制
echo $counter % 5 === 0 ? '' : $lastReview;

如果$counter除以5的余数等于0,那么php将不会回显评论,否则,它将回显。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58379218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档