【问题标题】:Rerun loop after every third post? (Wordpress)每第三个帖子后重新运行循环? (WordPress)
【发布时间】:2015-06-12 16:59:37
【问题描述】:

我的模板 (wordpress) 有问题。 我想创建一个包含 3 列的投资组合页面,并且可以在我的投资组合页面中显示帖子(无需跳转新页面)。我需要在每三个帖子之后重复这三个帖子。我将为我的重复帖子分配“隐藏”类,当单击该列时,类应设置为“块”。 我有一个代码:

<?php get_header(); ?>
<section> <div class="container container-bazar container-gallery"><?php 
$array = array();
    $count = 0;
    $i = 0;
$args = array(
    'posts_per_page' =>  -1,
    'post_type' => 'gallery',
);
$gallery = new WP_Query( $args );
if($gallery->have_posts()) :
while($gallery->have_posts()) :
        $gallery->the_post(); ?>
<div class="col-1 boxes<?php if( $count%3 == 0 ) { echo '-1'; }; $count++; ?>">
    <div class="post" id="post-<?php the_ID(); ?>">
        <figure class="indent-bot">
            <a href="<?php the_permalink(); ?>" rel="nofollow">
                <?php the_post_thumbnail(array(380,220,true)); ?>
            </a>
        </figure>
        <div class="col-1-content">
            <strong class="title-3">
                <a href="<?php the_permalink(); ?>" rel="nofollow">
                    <?php the_title(); ?>
                </a>
            </strong>
            <div class="entry">
                <a href="<?php the_permalink(); ?>" rel="nofollow">
                    <?php the_excerpt(); ?>
                </a>
            </div><!-- .entry -->
        </div><!-- .col-1-content-->
    </div><!-- .post -->
</div> <!-- .boxes -->
<?php endwhile; ?>
<?php while($gallery->have_posts()) :
$gallery->the_post();?>
<?php $imgaddr1 = get_post_meta($post->ID, 'imgaddr1', true);
$imgaddr2 = get_post_meta($post->ID, 'imgaddr2', true);
$imgssilka1 = get_post_meta($post->ID, 'imgssilka1', true);
$imgssilka2 = get_post_meta($post->ID, 'imgssilka2', true);
$namecolor1 = get_post_meta($post->ID, 'namecolor1', true);
$namecolor2 = get_post_meta($post->ID, 'namecolor2', true);
$numbercolor1 = get_post_meta($post->ID, 'numbercolor1', true);
$numbercolor2 = get_post_meta($post->ID, 'numbercolor2', true); ?>
</div>
        <div class="full clearfix">
            <div class="inner">
                <figure class="indent-bot1">
                    <a href="<?php the_permalink(); ?>" rel="nofollow">
                        <?php the_post_thumbnail(array(960,690)); ?>
                    </a>
                </figure>
                <div class="row">
                    <div class="col-md-5">
                        <div class="inf-1">
                            <h4>Информация</h4>
                        </div>
                        <div class="inf-2">
                            <h5><?php the_title(); ?></h5>
                            <div class="desc">
                                <?php the_excerpt(); ?>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="col-md-7 border-left">
                        <div class="inf-1">
                            <h4>Приложенные Цвета</h4>
                        </div>
                        <div class="inf-2">
                            <ul>
                                <li class="first-child">
                                    <a href="<?php echo $imgssilka1; ?>" class="img-block">
                                        <img src="<?php echo $imgaddr1; ?>">
                                    </a>
                                    <div class="txt">
                                        <strong><?php echo $namecolor1; ?></strong>
                                        <span><?php echo $numbercolor1; ?></span>
                                    </div>
                                </li>
                                <li class="last-child">
                                    <a href="<?php echo $imgssilka2; ?>" class="img-block">
                                        <img src="<?php echo $imgaddr2; ?>">
                                    </a>
                                    <div class="txt">
                                        <strong><?php echo $namecolor2; ?></strong>
                                        <span><?php echo $numbercolor2; ?></span>
                                    </div>
                                </li>
                            </ul>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div><!-- .inner -->
        </div>
<div class="container container-bazar container-gallery">
<?php endwhile; 
else: 
endif; ?>
</div><!-- .container -->
</section>
<?php get_footer(); ?>

但此代码按顺序显示帖子。

【问题讨论】:

  • 好吧,尝试使用这个函数codex.wordpress.org/Template_Tags/get_posts而不是WP_Query来删除$count。请问你:你想要三列有类似的数据,并且每三个帖子之后必须重复数据,不是吗?
  • 没有足够的声望来加入聊天)是的,我希望循环加载一个 3 列的帖子行,并在列表下方重复加载这 3 个帖子。之后,循环必须在接下来的 3 个帖子中重复,直到所有帖子结束。列表中的帖子包含其他自定义字段(它包含有关帖子的详细信息)。我将隐藏列表,直到用户单击该行中的项目 - 并且列表中的类似帖子变为活动状态并显示详细信息。

标签: php wordpress


【解决方案1】:
$i = 1;
//added before to ensure it gets opened
echo '<div>';
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
     // post stuff...

     // if multiple of 3 close div and open a new div
     if($i % 3 == 0) {echo '</div><div>';}

$i++; endwhile; endif;
//make sure open div is closed
echo '</div>';

【讨论】:

    【解决方案2】:

    这是一个非常不寻常的设置,让我思考。有一种方法不需要重新运行循环

    这里是如何

    • 您只需运行循环一次。代替默认循环,我们将从查询中提取我们的帖子数组,并通过foreach 循环运行我们的帖子。这就是我们要开始的地方

    • 我们需要拆分我们的内容,以便我们可以得到两个包含帖子数据的块,这需要保存到我们稍后将使用的数组中。为此,构建两个连接的数据字符串(一个字符串包含第一个数据块,第二个字符串包含第二个数据块),它们将保存在两个单独的变量中。

    • 完成后,我们需要添加 div 以形成包含三个帖子的帖子块,每个帖子都有一个唯一的类。这适用于两组字符串

    • 1234563来自字符串 2 等的相同三个帖子 1234563 p>

    这里是代码

    在我发布代码之前只需要一两个注释

    • 您需要修改类等以满足您的需求

    • 代码未经过全面测试,但 div 块和排序按预期工作

    • 我已经对代码进行了注释,以便于理解

    最后是代码

    $args = array(
        'posts_per_page' =>  -1,
        'post_type' => 'gallery',
    );
    $gallery = new WP_Query( $args );
    
    // Check if we have posts before we continue
    if( $gallery->have_posts() ) {
    
        // Use the array of posts and a foreach loop to build out super array
        foreach ( $gallery->posts as $key=>$post ) {
            // Setup postdata so we can make use of template tags
            setup_postdata( $post );
    
            // Setup/define our first variable which will hold our first set of post data
            $output = '';
    
            // Open a new div on the first post and every 3rd one there after to hold three posts
            if ( $key%3 == 0 ) {
                // We will call this class "first-x" where x represents the block count
                $output .= '<div class="first-' . floor( $key / 3 ) . '">';
            }
    
            // Concatenate your first loop into a string to our first variable $output
            $output .= '<div class="post" id="post-' . $post->ID . '">
                    <figure class="indent-bot">
                        <a href="' . get_the_permalink() . '" rel="nofollow">
                            ' . get_the_post_thumbnail( $post->ID, array( 380,220,true ) ) . '
                        </a>
                    </figure>
                    <div class="col-1-content">
                        <strong class="title-3">
                            <a href="' . get_the_permalink() . '" rel="nofollow">
                                ' . get_the_title() . '
                            </a>
                        </strong>
                        <div class="entry">
                            <a href="' . get_the_permalink() . '" rel="nofollow">
                                ' . get_the_excerpt() . '
                            </a>
                        </div><!-- .entry -->
                    </div><!-- .col-1-content-->
                </div><!-- .post -->
            </div> <!-- .boxes -->';
    
            // Add our closing div after every third post or the last post if there is less than three
            if ( $key%3 == 2 || !array_key_exists( ( $key + 1 ), $gallery->posts ) ) {
                $output .= '</div>';
            }
    
            // Create our new array of post data split in two and use with new array keys
            $new_posts_array[floor( $key / 3 ) * 3 + $key] = $output;
    
            // Setup/define our second variable which will hold the second set of post data from our posts
            // This is the set that you would like to hide
            $output_1 = '';
    
            // Open a new div on the first post and every 3rd one there after to hold three posts
            if ( ( $key%3 ) == 0 ) {
                // This block of posts will use class "second-x" where x represents the block count
                $output_1 .= '<div class="second-' . floor( $key / 3 ) . '">';
            }
    
            $imgaddr1     = get_post_meta( $post->ID, 'imgaddr1',     true );
            $imgaddr2     = get_post_meta( $post->ID, 'imgaddr2',     true );
            $imgssilka1   = get_post_meta( $post->ID, 'imgssilka1',   true );
            $imgssilka2   = get_post_meta( $post->ID, 'imgssilka2',   true );
            $namecolor1   = get_post_meta( $post->ID, 'namecolor1',   true );
            $namecolor2   = get_post_meta( $post->ID, 'namecolor2',   true );
            $numbercolor1 = get_post_meta( $post->ID, 'numbercolor1', true );
            $numbercolor2 = get_post_meta( $post->ID, 'numbercolor2', true ); 
    
            // Concatenate your second set of post data into a string to our second variable $output_1
            $output_1 .= '<div class="full clearfix">
                <div class="inner">
                    <figure class="indent-bot1">
                        <a href="' . get_the_permalink() . '" rel="nofollow">
                            ' . get_the_post_thumbnail(  $post->ID, array( 960, 690 ) ) . '
                        </a>
                    </figure>
                    <div class="row">
                        <div class="col-md-5">
                            <div class="inf-1">
                                <h4>Информация</h4>
                            </div>
                            <div class="inf-2">
                                <h5>' . get_the_title() . '</h5>
                                <div class="desc">
                                    ' . get_the_excerpt() . '
                                </div>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                        <div class="col-md-7 border-left">
                            <div class="inf-1">
                                <h4>Приложенные Цвета</h4>
                            </div>
                            <div class="inf-2">
                                <ul>
                                    <li class="first-child">
                                        <a href="' . $imgssilka1 . '" class="img-block">
                                            <img src="' . $imgaddr1 . '">
                                        </a>
                                        <div class="txt">
                                            <strong>' . $namecolor1 . '</strong>
                                            <span>' . $numbercolor1 . '</span>
                                        </div>
                                    </li>
                                    <li class="last-child">
                                        <a href="' . $imgssilka2 . '" class="img-block">
                                            <img src="' . $imgaddr2 . '">
                                        </a>
                                        <div class="txt">
                                            <strong>' . $namecolor2 . '</strong>
                                            <span>' . $numbercolor2 . '</span>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                            <div class="clearfix"></div>
                        </div>
                    </div>
                </div><!-- .inner -->
            </div>';
    
            // Add our closing div after every third post or the last post if there is less than three
            if ( $key%3 == 2 || !array_key_exists( ( $key + 1 ), $gallery->posts ) ) {
                $output_1 .= '</div>';
            }
    
            // Create our new array of post data split in two and use with new array keys
            $new_posts_array[( floor( $key / 3 ) + 1 ) * 3 + $key] = $output_1;         
    
    
        }
        wp_reset_postdata();
    
        // Sort our new array so that the keys are numerical again
        ksort( $new_posts_array );
    
        // Run a foreach loop to output our posts as we need. No need to modify anything here
        foreach ( $new_posts_array as $v )
            echo $v;
    }
    

    【讨论】:

    • 抱歉让您久等了。感谢你的回答。我认为你有一个很好的解决方案。此时我有另一种解决方案(请参阅我的答案)。我不确定它是如何正确的,但它确实有效。你可以评论一下吗?
    【解决方案3】:

    众所周知,WordPress 是一个开源工具,所有插件都可用于管理此类格式。

    我建议使用插件来管理您的需求。我使用columns plugin 进行格式化输出。

    【讨论】:

    • 这个插件不适合
    【解决方案4】:

    我得到了记录的总数并进行了循环。 在循环内部,两个循环。 第一个循环显示一个表格。第二个循环显示一个列表。

    <section>
    <div class="container container-gallery">
        <?php 
            $offset = 0;
            $offset1 = 0;
            $i =0;
            $count = 0;
            $reset =0;
            $reset1 = 0;
            $args = array(
                'posts_per_page' =>  -1,
                'post_type' => 'gallery',
            );
            $gallery = new WP_Query( $args );
            $numberposts = $gallery->post_count;
        if ($numberposts){
                $id1=0;
                $id2=0;
                while($count < $numberposts){
                //          print_r($arr1);
                $count++;
                //echo "<h2>".$count."</h2>";
                $arr1 = array(
                    'posts_per_page' =>  400,
                    'post_type' => 'gallery',
                    'offset'=>$offset
                );
    
                $arr2 = array(
                    'posts_per_page' =>  400,
                    'post_type' => 'gallery',
                    'offset'=>$offset1
                );
    
                $loop1 = new WP_Query($arr1);
                $loop2 = new WP_Query($arr1);
    
    
                    while($loop1->have_posts()) : $loop1->the_post(); 
                    if ($reset<3) :
                        $reset++;               
        ?>
        <?php
            $colorfilter1 = get_post_meta($post->ID, 'checkboxwhite', true);
            $colorfilter2 = get_post_meta($post->ID, 'checkbox_beige', true);
            $colorfilter3 = get_post_meta($post->ID, 'checkbox_brown', true);
            $colorfilter4 = get_post_meta($post->ID, 'checkbox_gray', true);
            $colorfilter5 = get_post_meta($post->ID, 'checkbox_black', true);
            $colorfilter6 = get_post_meta($post->ID, 'checkbox_vvid', true);
            if ($colorfilter1 != "") $colorfilter1 ="white ";
            if ($colorfilter2 != "") $colorfilter2 ="beige ";
            if ($colorfilter3 != "") $colorfilter3 ="brown ";
            if ($colorfilter4 != "") $colorfilter4 ="gray ";
            if ($colorfilter5 != "") $colorfilter5 ="black ";
            if ($colorfilter6 != "") $colorfilter6 ="vivid ";
            $class_color = $colorfilter1.$colorfilter2.$colorfilter3.$colorfilter4.$colorfilter5.$colorfilter6;
        ?>
                    <div class="col-1 mcol boxes<?php if( $i%3 == 0 ) { echo '-1'; }; $i++; echo ' '.$class_color;?>" id="colbox<?php echo $id1; $id1++;?>"  data-id="click" >
                        <div class="post" id="post-<?php the_ID(); ?>">
                            <figure class="indent-bot">
    
                                    <?php the_post_thumbnail(array(380,220,true)); ?>
    
                            </figure>
                            <div class="col-1-content">
                                <strong class="title-3">
    
                                        <?php  the_title(); ?>
    
                                </strong>
                                <div class="entry">
    
                                        <?php the_excerpt(); ?>
    
                                </div><!-- .entry -->
                            </div><!-- .col-1-content-->
                        </div><!-- .post -->
                    </div><!-- .boxes -->           
                    <?php else : break;?>
                    <?php endif; ?>
                    <?php endwhile; ?>
                    <?php
                        $reset = 0;
                        $offset +=3; 
                    ?>  
                    <?php wp_reset_postdata(); ?>
    
                    <?php
                        while($loop2->have_posts()) : $loop2->the_post(); 
                        if ($reset1<3) :
                            $reset1++;  
                    ?>
                    <?php
                        $numbercolor1 = get_post_meta($post->ID, 'numbercolor1',true);
                        $numbercolor2 = get_post_meta($post->ID, 'numbercolor2', true);
                        $imgaddr1 = get_post_meta($post->ID, 'imgaddr1', true);
                        $imgaddr2 = get_post_meta($post->ID, 'imgaddr2', true);
                        $imgssilka1 = get_post_meta($post->ID, 'imgssilka1', true);
                        $imgssilka2 = get_post_meta($post->ID, 'imgssilka2', true);
                        $namecolor1 = get_post_meta($post->ID, 'namecolor1', true);
                        $namecolor2 = get_post_meta($post->ID, 'namecolor2', true);
                    ?>
                    </div>
                            <div class="full clearfix active colbox<?php echo $id2; $id2++;?>" id="">
                                <div class="inner">
                                    <figure class="indent-bot1">
                                        <a href="<?php the_permalink(); ?>" rel="nofollow">
                                            <?php the_post_thumbnail(array(960,690)); ?>
                                        </a>
                                    </figure>
                                    <div class="row">
                                        <div class="col-md-5">
                                            <div class="inf-1">
                                                <h4>Информация</h4>
                                            </div>
                                            <div class="inf-2">
                                                <h5><?php the_title(); ?></h5>
                                                <div class="desc">
                                                    <?php the_excerpt(); ?>
                                                </div>
                                            </div>
                                            <div class="clearfix"></div>
                                        </div>
                                        <div class="col-md-7 border-left">
                                            <div class="inf-1">
                                                <h4>Приложенные<</h4>
                                            </div>
                                            <div class="inf-2">
                                                <ul>
                                                    <li class="first-child">
                                                        <a href="<?php echo $imgssilka1; ?>" class="img-block">
                                                            <img src="<?php echo $imgaddr1; ?>">
                                                        </a>
                                                        <div class="txt">
                                                            <strong><?php echo $namecolor1; ?></strong>
                                                            <span><?php echo $numbercolor1; ?></span>
                                                        </div>
                                                    </li>
                                                    <li class="last-child">
                                                        <a href="<?php echo $imgssilka2; ?>" class="img-block">
                                                            <img src="<?php echo $imgaddr2; ?>">
                                                        </a>
                                                        <div class="txt">
                                                            <strong><?php echo $namecolor2; ?></strong>
                                                            <span><?php echo $numbercolor2; ?></span>
                                                        </div>
                                                    </li>
                                                </ul>
                                            </div>
                                            <div class="clearfix"></div>
                                        </div>
                                    </div>
                                    <div class="row">
                                            <div class="col-md-12">
                                                <div class="c_btn"></div>
                                            </div>
                                        </div>
                                </div><!-- .inner -->
                            </div>
                    <div class="container container-gallery">
                    <?php else : break;?>
                    <?php endif; ?>
                    <?php endwhile; ?>
                    <?php
                        $reset1 = 0;
                        $offset1 +=3; 
                    ?>
                    <?php wp_reset_postdata(); ?>
    
        <?php 
    
            } //end if ($count <= $numberposts)
        } //end if ($numberposts)
        ?>
    
        <?php 
        if ( have_posts() ) while ( have_posts() ) : the_post(); // старт цикла ?>
        <article id="post-<?php the_ID(); ?>"> 
            <?php the_content();  ?>
        </article>
    <?php endwhile;  ?>
    </div><!-- .container -->
    </section>
    

    【讨论】:

    • 这有点低效,因为您正在运行两个单独的查询。这意味着您有两倍的数据库调用,这会减慢您的页面加载时间(它的加载速度几乎是两倍)。我看到你查询了 400 个帖子,这导致了近一千个数据库查询
    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多