在自定义POST类型中,每x个POST输出不同的循环

时间:2016-02-09 作者:Laura Sage

我在几年前的另一个StackExchange问题中也提到了这一点,当时的布局大致如下:

<div class=items active>
    <div class=row>

        <div class=1b>
            --content of first post in loop--
        </div>

        <div class=1c>
            --content of second post in loop--
        </div>

        <div class=1d>
            --content of third post in loop--
        </div>

        <div class=1e>
            --content of fourth post in loop--
        </div>

    </div>
</div>

<div class=items> -- Note the lack of the active class for this div and row --
    <div class=row>

        <div class=2b>
            --content of fifth post in loop--
        </div>

        <div class=2c>
            --content of sixth post in loop--
        </div>

        <div class=2d>
            --content of seventh post in loop--
        </div>

        <div class=2e>
            --content of eighth post in loop--
        </div>

    </div>
</div>
但是,解决方案似乎没有使用任何自定义帖子类型。

下面是我的代码,试图修改另一个线程中提供的代码,但我的循环中根本没有显示任何内容(请注意,我认为原始线程的更改是每3篇帖子发生一次,而不是像我在这里看到的每4篇帖子,因此我很可能在下面的数字有误:(

add_shortcode( \'partnerlogos\', \'display_custom_post_type\' );

function display_custom_post_type(){
    $args = array(
        \'post_type\' => \'dnf_partner_logos\',
        \'post_status\' => \'publish\'
    );

    $query = new WP_Query( $args );
        while( have_posts() ) : the_post();
        $image = get_field(\'partner_logo\');

        $position = $wp_query->current_post;
        $p3 = $position%4;
        $p6 = $position%8;
        $number_posts = $wp_query->post_count-1;

        // vars
        $url = $image[\'url\'];
        $title = $image[\'title\'];
        $alt = $image[\'alt\'];
        $caption = $image[\'caption\'];

        // thumbnail
        $size = \'dnf-partners\';
        $thumb = $image[\'sizes\'][ $size ];
        $width = $image[\'sizes\'][ $size . \'-width\' ];
        $height = $image[\'sizes\'][ $size . \'-height\' ];

        if( $p3 == 0 ) echo \'<div class="item active"><div class="row">\';
        if( $p6 == 0 ) echo \'<div class="col-md-3">\';
        elseif( $p6 == 1 || $p6 == 3) echo \'<div class="item"><div class="row"\';
        elseif( $p6 == 5 ) echo \'<div class="col-md-3">\'; ?>

        <img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>"/><!--item-->

        <?php if( $position == $number_posts || in_array($p6, array(0, 2, 4, 5)) ) echo \'</div>\';
            if( $p3 == 2 || $position == $number_posts ) echo \'</div></div><!--layer-->\'; 

        endwhile;
}

1 个回复
SO网友:Laura Sage

经过多次尝试和错误,我为WordPress循环正确设置了代码。我把它写进了一个短代码。这是:

add_shortcode( \'partnerlogos\', \'display_partner_logos\' );
function display_partner_logos(){
    // Define the query
    $args = array(
        \'post_type\' => \'dnf_partner_logos\',
        \'post_status\' => \'publish\'
    );

    $query = new WP_Query( $args ); ?>

<!-- Carousel
================================================== -->
<div id="partnersCarousel" class="carousel slide">
    <div class="carousel-inner">
        <?php $count = 0;
        while ($query->have_posts()) : $query->the_post();
            $image = get_field(\'partner_logo\');
            $url = $image[\'url\'];
            $size = \'dnf-partners\';
            $thumb = $image[\'sizes\'][ $size ];
            $width = $image[\'sizes\'][ $size . \'-width\' ];
            $height = $image[\'sizes\'][ $size . \'-height\' ];

        if($count % 4 == 0) { ?>

        <div class="item <?php if($count == 0) echo "active"; ?>">
            <div class="row">

        <?php } ?>

                <div <?php post_class("col-md-3") ?> id="post-<?php the_ID(); ?>">
                    <a href="#" class="thumbnail">
                        <img src="<?php echo $thumb; ?>" class="img-responsive" alt="Thumb11">
                    </a>
                </div>

        <?php if($count % 4 == 3) { ?>   
            </div>
        </div>

        <?php } 
            $count++;
        endwhile;

        if($count % 4 != 0) { ?>
        </div>
    </div>
    <?php } ?>

</div>

<a class="left carousel-control" href="#partnersCarousel" data-slide="prev"><i class="fa fa-chevron-left fa-2x"></i></a>
<a class="right carousel-control" href="#partnersCarousel" data-slide="next"><i class="fa fa-chevron-right fa-2x"></i></a>

</div>

<?php     
    wp_reset_postdata();
    wp_reset_query();
}