PHP不在列中输出

时间:2018-05-29 17:12:10

标签: php twitter-bootstrap

我正在尝试在col-sm-4中输出我的文字,但只有第一项会进入col-sm-4

其余部分无序

这是我的代码:

<div class="col-sm-6">              
                <?php 
                    // Team ophalen
                    $team_posts = get_posts( array(
                        'post_type' => 'team',
                        'posts_per_page' => -1,
                        'orderby' => 'title',
                    ) );

                    if ( $team_posts ):
                ?>
                <div class="col-sm-4">
                <?php 
                foreach ( $team_posts as $post ): 
                setup_postdata($post);

                // Foto URL
                $thumb_src = null;
                if ( has_post_thumbnail($post->ID) ) {
                    $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'team-thumb' );
                    $thumb_src = $src[0];
                }
                ?>
                    <div class="card" <?php if ( $thumb_src ): ?>style="background-image: url('<?php echo $thumb_src; ?>');"<?php endif; ?>>
                        <div class="card-name">
                            <span class="text-center underline"><?php the_title(); ?></span>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
                <?php endif; ?>
            </div>

1 个答案:

答案 0 :(得分:1)

正如@castis所说,你的col-sm-4不是循环的一部分。

这应该有用。

<?php
    $team_posts = get_posts(array(
        'post_type' => 'team',
        'post_per_page' => -1,
        'orderby' => 'title',
    ));

    if($team_posts);
?>

<?php
foreach($team_posts as $post):
    setup_pustdata($_POST);
    $thumb_src = null;

    if(has_post_thumbnail($post->ID)) {
        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'team-thumb');
        $thumb_src = $src[0];
    }
?>

<div class="col-sm-4">
    <div class="card" <?php if ($thumb_src): ?>style="background-image: url('<?php echo $thumb_src; ?>');"<?php endif; ?>>
        <div class="card-name">
            <span class="text-center underline"><?php the_title(); ?></span>
        </div>
    </div>
</div>

<?php endforeach; ?>
<?php endif; ?>