如何在旋转木马幻灯片上显示4列2行的8个帖子?

时间:2021-07-25 作者:codieboie

我正在尝试实现网格布局,以便在旋转木马滑块上可以看到帖子。每张幻灯片由8根立柱组成。每行4个立柱。

我也在使用bootstrap。

我希望html看起来像:

<div class="slider_container">
<div class="services-slide">
<div class="row">
<div class="col-md-3">
<div class="box-desc">
<h4>title 1</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 2</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 3</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 4</h4>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="box-desc">
<h4>title 5</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 6</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 7</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 8</h4>
</div>
</div>
</div>
<div class="services-slide">
<div class="row">
<div class="col-md-3">
<div class="box-desc">
<h4>title 9</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 10</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 11</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 12</h4>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="box-desc">
<h4>title 13</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 14</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 15</h4>
</div>
<div class="col-md-3">
<div class="box-desc">
<h4>title 16</h4>
</div>
</div>
</div>
</div>
这是我的代码,但它不起作用。

<div class="slider_container">
<?php
ob_start();
$args = array( \'post_type\' => \'services\',\'posts_per_page\' =>-1,\'order\' => \'ASC\');
$the_query = new WP_Query($args);
if ($the_query->have_posts()) : 
$i = 0;
$o = 0;
  ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="services-slide">
 <div class="row">
   <?php if ($i == 4) { $i = 0; ?>
    </div>
    <div class="row">
   <?php } ?>
   <div class="col-md-3">
       <div class="box-desc">
            <h4><?php echo get_the_title(); ?></h4>
        </div>
    </div>
</div>
  <?php $i++; ?>
</div>
<?php endwhile; ?>
<?php
endif;
wp_reset_postdata();
$content = ob_get_contents();
ob_end_clean();
return $content; ?>
</div>
请帮忙。

1 个回复
SO网友:Buttered_Toast

从HTML代码示例中很难理解您想要实现什么,但通过php,您可以执行以下操作。

<?php
$posts = new WP_Query([
    \'post_type\'      => \'services\',
    \'posts_per_page\' => -1,
    \'order\'          => \'ASC\'
]);

if ($posts->have_posts()) :
?>
<ul class="slider-container">
    <?php
    $loop_position = 1;
    while ($posts->have_posts()) {
        $posts->the_post();

        // if loop position is greater than our threshold (8) reset to initial position (1)
        if ($loop_position > 8) $loop_position = 1;

        // if we are in the first loop (loop position 1) itiration create the slide open tag
        if ($loop_position === 1) echo \'<li class="single-slide">\';

        // if we are in the first/fifth loop (loop position 1/5) itiration create the row open tag
        if (in_array($loop_position, [1,5])) echo \'<div class="row">\';

        // here we can simply output each loop itiration html as we need
        echo \'<div class="col-md-3">\';
        echo     \'<div class="box-desc">\';
        echo         "<h4>Some title {$loop_position}</h4>";
        echo     \'</div>\';
        echo \'</div>\';

        // if we reached the end of the loop (no more post) we need to close all the tags
        // starting from row than single-slide
        if ($posts->found_posts == $loop_position) {
           echo \'</div></li>\';
           
           // to prevent the rest of the code from executing we exit out of the loop, using break
           break;
        }

        // if we are in the fourth/eighth loop (loop position 4/8) itiration create the row closing tag
        if (in_array($loop_position, [4,8])) echo \'</div>\';

        // if we are in the last loop (loop position 8) itiration create the slide closing tag
        if ($loop_position === 8) echo \'</li>\';

        // increate loop position by 1
        $loop_position++;
    }
    ?>
</ul>
<?php
endif;
wp_reset_postdata();
?>
HTML输出如下

<ul class="slider-container">
    <li class="single-slide">
        <div class="row">
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 1</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 2</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 3</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 4</h4>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 5</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 6</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 7</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 8</h4>
                </div>
            </div>
        </div>
    </li>
    <li class="single-slide">
        <div class="row">
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 1</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 2</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 3</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 4</h4>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 5</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="box-desc">
                    <h4>Some title 6</h4>
                </div>
            </div>
        </div>
    </li>
</ul>
正如我们决定的那样,每八(8)个帖子都会收到自己的幻灯片,在每个幻灯片中,每四(4)个帖子都会用<div class="row">...</div> 标签

我个人认为这有点过分了,太复杂了,你可以不使用引导程序来布局,而使用css来达到同样的效果grid.

如果您愿意,我可以编辑我的答案以包括非引导布局

相关推荐

如何在unction.php中更改图片url?

例如,我的图像url是https://aa.com/wp-content/uploads/2021/07/x-600x600.jpg我想把它改成https://images.s3.us-east-2.amazonaws.com/aa.com/wp-content/uploads/2021/07/x-600x600.jpg或https://images.s3.us-east-2.amazonaws.com/aa.com/wp-content/uploads/2021/07/x-600x600.jpg.web