如何动态添加活动类

时间:2020-07-17 作者:Johny

enter image description here**我使用此代码但不起作用**

<div class="row">
        <!-- Featured Post Area -->
        <div class="col-12">
            <div class="featured-post-area">
                <div id="featured-post-slides" class="carousel slide d-flex flex-wrap" data-ride="carousel">
                    <div class="carousel-inner">
                        <?php 
                            $feuted_post_slider = new WP_Query( array(
                                \'post_type\'         => \'post\',
                                \'posts_per_page\'    => 5,
                                \'order\'             => \'ASC\',
                                // \'meta_key\'          => \'featured_post\',
                                // \'meta_value\'        => \'on\'
                            ) );
                            // need a variable for active class
                            $i = 1;
                        ?>
                        <?php if( $feuted_post_slider->have_posts() ) : ?>
                            <?php while( $feuted_post_slider->have_posts() ) : $feuted_post_slider->the_post(); 
                            
                            $featured_img_id    = get_post_thumbnail_id( get_the_ID() );
                            $featured_img       = wp_get_attachment_image_src( $featured_img_id , \'full\' );
                            ?>
                                <div class="carousel-item <?php if ( $i == 1 ) : echo \'active\'; endif; ?> bg-img" style="background-image: url(<?php echo $featured_img[0]; ?>)">
                                    <!-- Featured Post Content -->
                                    <div class="featured-post-content">
                                        <p><?php the_time( \'M d, Y\' ); ?> / 
                                            <?php 
                                                $categoris = get_the_category();
                                                $separator = \', \';
                                                $output    = \'\';
    
                                                if( $categoris ){
                                                    foreach( $categoris as $category ){
                                                        $output .= \'<a href="\' . get_category_link( $category->term_id ) . \'">\' . $category->cat_name .\'</a>\' . $separator;
                                                    }
                                                    echo trim( $output , $separator );
                                                }
                                            ?>
                                            <!-- end category here -->
                                        </p>
                                        <a href="<?php the_permalink(); ?>" class="post-title">
                                            <h2><?php the_title(); ?></h2>
                                        </a>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php endif; wp_reset_postdata(); ?>
    
                        <!-- end post slider here -->
                    </div>
    
                    <ol class="carousel-indicators">
                        <?php 
                        $post_num = 1;
                        $slider_data_value = 0;
                        $count = 0;                           
                        if( $feuted_post_slider->have_posts() ) : ?>
                            <?php while( $feuted_post_slider->have_posts() ) : $feuted_post_slider->the_post(); ?>
                                <li data-target="#featured-post-slides" data-slide-to="<?php echo $slider_data_value++; ?>" class="<?php 
                                    if($count==0){
                                        echo "active";  
                                      }
                                      else{
                                          echo " ";
                                      }
                                ?>">
                                    <h2><?php echo $post_num++; ?></h2>
                                    <a href="<?php the_permalink(); ?>" class="post-title">
                                    <h5><?php the_title(); ?></h5>
                                    </a>
                                </li>
                            <?php endwhile; ?>
                        <?php endif; wp_reset_postdata(); ?>
    
                    </ol>
                
                </div>
            </div>
        </div>
    </div>

enter image description here

1 个回复
最合适的回答,由SO网友:Tom Broucke 整理而成

你应该改变$i == 1$i++ == 1

相关推荐

在WordPress中将值从输入字段传递到php函数?

当我从下拉菜单中选择类别时,它会在输入字段中正确更新。理想情况下,它应该更新recipeIndex中的配方过滤器。php。我尝试用方法“将输入包装在表单中”;获取(&Q);但这仍然不起作用。HTML位于配方索引页面上的HTML小部件中。CSS、JavaScript和PHP代码在单独的代码段中(我使用Woody snippets Wordpress插件https://wordpress.org/plugins/insert-php/)请帮忙。HTML<div class="dropdown_