Php 为前三个元素指定一个唯一的id,然后对下三个元素重复此操作

Php 为前三个元素指定一个唯一的id,然后对下三个元素重复此操作,php,html,wordpress,Php,Html,Wordpress,我有一行代码,可以从某个帖子类型生成帖子: <div class="row"> <?php $args = array( 'post_type' => 'cases', 'posts_per_page' => 3, offset => 2 ); $the_query1

我有一行代码,可以从某个帖子类型生成帖子:

    <div class="row">

        <?php
            $args = array(
                'post_type' => 'cases',
                'posts_per_page' => 3,
                offset => 2
            );
            $the_query1 = new WP_Query( $args );
            if ( $the_query1 -> have_posts() ):
            while ( $the_query1->have_posts() ): $the_query1->the_post();
        ?>

        <div class="col-sm-4">
            <div class="case-container">
                <div class="case-info-triple">
                    <h4 id="case-title"><?php the_title(); ?></h4>
                    <p id="case-type"><?php the_field('case_type') ?></p>
                    <p id="case-desc"><?php $caseteaser = get_field('case_content', false, false); echo substr($caseteaser, 0, 200); ?></p>
                    <img src="Diensten - zeo9-arrow.png" /><a href="<?php the_field('case_url') ?>">Bekijk deze case</a>
                </div>
            </div>
        </div>

        <?php endwhile; wp_reset_postdata();?>
        <?php endif;?>

    </div>

这就产生了:

但是,我希望每三个“
案例信息”三元组都有自己的唯一id。例如,
,然后再次,
等等

见相应来源:

我们现在每页显示3篇文章,但将来我们会将其更改为9篇。基本上,每个第一、第二和第三个信息元素都有自己的颜色,我需要它们有一个唯一的id,这样我就可以用css为它们设置样式


如何实现这一点?

在while循环之前,初始化一个变量:

$cnt = 0;
然后在while循环中,您可以放置以下内容:

$classID = $cnt++%3;
将计数器和模数增加3。您可以针对任意数量的列进行调整。使用3作为模数将产生一个介于0和2之间的数字。然后在此处使用classID:

<div class="case-info-triple<?php echo '-'.$classID; ?>">

在while循环之前,初始化一个变量:

$cnt = 0;
然后在while循环中,您可以放置以下内容:

$classID = $cnt++%3;
将计数器和模数增加3。您可以针对任意数量的列进行调整。使用3作为模数将产生一个介于0和2之间的数字。然后在此处使用classID:

<div class="case-info-triple<?php echo '-'.$classID; ?>">

有许多选项,但您可以使用计数器变量并将其添加到类名中以使其唯一,例如,在代码中,您可以这样做:-

<div class="row">

<?php
    $args = array(
        'post_type' => 'cases',
        'posts_per_page' => 3,
        offset => 2
    );
    $the_query1 = new WP_Query( $args );
    if ( $the_query1 -> have_posts() ):
    $id = 1;
    while ( $the_query1->have_posts() ): $the_query1->the_post();
    $infoClassName = "case-info-triple-"+$id;
?>

<div class="col-sm-4">
    <div class="<?php echo $infoClassName; ?>">
        <div class="case-info-triple">
            <h4 id="case-title"><?php the_title(); ?></h4>
            <p id="case-type"><?php the_field('case_type') ?></p>
            <p id="case-desc"><?php $caseteaser = get_field('case_content', false, false); echo substr($caseteaser, 0, 200); ?></p>
            <img src="Diensten - zeo9-arrow.png" /><a href="<?php the_field('case_url') ?>">Bekijk deze case</a>
        </div>
    </div>
</div>

<?php $id++; ?>
<?php endwhile; wp_reset_postdata();?>
<?php endif;?>


有许多选项,但您可以使用计数器变量并将其添加到类名中以使其唯一,例如,在代码中,您可以这样做:-

<div class="row">

<?php
    $args = array(
        'post_type' => 'cases',
        'posts_per_page' => 3,
        offset => 2
    );
    $the_query1 = new WP_Query( $args );
    if ( $the_query1 -> have_posts() ):
    $id = 1;
    while ( $the_query1->have_posts() ): $the_query1->the_post();
    $infoClassName = "case-info-triple-"+$id;
?>

<div class="col-sm-4">
    <div class="<?php echo $infoClassName; ?>">
        <div class="case-info-triple">
            <h4 id="case-title"><?php the_title(); ?></h4>
            <p id="case-type"><?php the_field('case_type') ?></p>
            <p id="case-desc"><?php $caseteaser = get_field('case_content', false, false); echo substr($caseteaser, 0, 200); ?></p>
            <img src="Diensten - zeo9-arrow.png" /><a href="<?php the_field('case_url') ?>">Bekijk deze case</a>
        </div>
    </div>
</div>

<?php $id++; ?>
<?php endwhile; wp_reset_postdata();?>
<?php endif;?>


能否向数组中添加指示行的参数(3行3个帖子)


然后在脚本中移动html,以便在。。。如果。。。else
部分自动命名您的
-2
、或
-3
,基于它所在的帖子行

能否向数组中添加指示行的参数(3行3个帖子)


然后在脚本中移动html,以便在。。。如果。。。else
部分自动命名您的
-2
、或
-3
,基于它所在的帖子行

下面的模解对你有用吗?是的,它似乎很有用。我在PHP中使用了非常简单的方法。令人印象深刻。下面的模解对你有用吗?是的,它似乎很有用。我在PHP中使用了非常简单的方法。给人印象深刻的