【问题标题】:Issue about slider recent posts in PHP关于 PHP 中滑块最近帖子的问题
【发布时间】:2018-05-23 21:31:12
【问题描述】:

我很尴尬,因为我正在使用滑块来显示最近的帖子。我正在使用 Carousel Bootstrap,这是我的代码:

<?php
        $lastposts = get_posts( array(
            'posts_per_page' => 3,
                'category' => 584
        ) );

        if ( $lastposts ) { ?>
            <div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
              <div class="carousel-inner">



            <?php foreach ( $lastposts as $post ) :
                setup_postdata( $post ); ?>
                        <div class="carousel-item active">
                        <img src="/wp-content/uploads/slider.png">
                                <div class="carousel-caption d-none d-md-block">
                                    <div class="title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </div>
                                </div>
                            </div>

            <?php
            endforeach;
            wp_reset_postdata();
        }
?>
</div>
</div>

问题是我所有的项目都是“活跃的”。

我该如何解决?仅将第一项设为活动状态。

【问题讨论】:

  • 添加计数器/标志。如果计数器/标志 == 0 - 添加类。

标签: php wordpress posts


【解决方案1】:

用计数器变量试试:

<?php
        $lastposts = get_posts( array(
            'posts_per_page' => 3,
                'category' => 584
        ) );

        if ( $lastposts ) { ?>
            <div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
              <div class="carousel-inner">


            <?php $counter = 1; ?>
            <?php foreach ( $lastposts as $post ) :
                setup_postdata( $post ); ?>
                        <div class="carousel-item <?php echo ($counter==1) ? "active" : ""; ?>">
                        <img src="/wp-content/uploads/slider.png">
                                <div class="carousel-caption d-none d-md-block">
                                    <div class="title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </div>
                                </div>
                            </div>

            <?php
                $counter++;
            endforeach;
            wp_reset_postdata();
        }
?>
</div>
</div>

【讨论】:

  • 修复 echo 部分。
  • 并非如此。 echo在哪里?
  • 现在?其实我不知道那个位置的回声是否正确。
  • 现在好了。
猜你喜欢
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
相关资源
最近更新 更多