向查询的第一个帖子添加类

时间:2016-09-29 作者:steamfunk

如何将is active类添加到第一个<li> 是否由查询生成?谢谢你,我很感激。

<?php
    $_terms = get_terms( array(\'claim-accordion-type\') );

    foreach ($_terms as $term) :

        $term_slug = $term->slug;
        $_posts = new WP_Query( array(
                    \'post_type\'         => \'claims_accordion\',
                    \'posts_per_page\'    => -1,
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'claim-accordion-type\',
                            \'field\'    => \'slug\',
                            \'terms\'    => $term_slug,
                        ),
                    ),
                ));

        if( $_posts->have_posts() ) :

 echo\'<li class="accordion-item " data-accordion-item>\';

        echo\'<a href="#" class="accordion-title">\';
            echo \'\'. $term->name .\'\';

        echo\'</a>\';






            while ( $_posts->have_posts() ) : $_posts->the_post();





        ?>

    <div class="accordion-content" data-tab-content>


      <a data-open="exampleModal1">
        <h4><?php the_title(); ?></h4></a>


    </div>


    <div class="reveal" id="exampleModal1" data-reveal>


      <?php the_content(); ?>

        <button class="close-button" data-close aria-label="Close modal" type="button">
          <span aria-hidden="true">&times;</span>
        </button>
    </div>



    <?php
        endwhile;


   echo \'</li>\';
        endif;
        wp_reset_postdata();




    endforeach;
    ?>

</ul>

2 个回复
最合适的回答,由SO网友:Ahmed Fouad 整理而成
<?php
    $_terms = get_terms( array(\'claim-accordion-type\') );
    $i = 0;
    foreach ($_terms as $term) :
    $term_slug = $term->slug;
    $_posts = new WP_Query( array(                        \'post_type\'         => \'claims_accordion\',                        \'posts_per_page\'    => -1,                        \'tax_query\' => array(                            array(                                \'taxonomy\' => \'claim-accordion-type\',                                \'field\'    => \'slug\',                                \'terms\'    => $term_slug,                            ),                        ),                    ));

    if( $_posts->have_posts() ) :
    $i++;

    if ( $i == 1 ) {
        $class = \'is-active\';
    } else {
        $class = null;
    }

    echo\'<li class="accordion-item \' . $class . \'" data-accordion-item>\';
    echo\'<a href="#" class="accordion-title">\';
    echo \'\'. $term->name .\'\';
    echo\'</a>\';
    while ( $_posts->have_posts() ) :
    $_posts->the_post();
    ?>
        <div class="accordion-content" data-tab-content>
          <a data-open="exampleModal1">
            <h4><?php  the_title(); ?></h4></a>
        </div>
        <div class="reveal" id="exampleModal1" data-reveal>
          <?php  the_content(); ?>
            <button class="close-button" data-close aria-label="Close modal" type="button">
              <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <?php
    endwhile;
    echo \'</li>\';
    endif;
    wp_reset_postdata();
    endforeach;
    ?>
</ul>
?>
SO网友:T.Todua

另一种方式-而不是:

echo\'<li class="accordion-item " data-accordion-item>\';
使用:

echo \'<li class="accordion-item \'.(!defined(\'firstp_created_444\') && define(\'firstp_created_444\',true) ? \'is-active\' : \'\') .\'" data-accordion-item>\';

相关推荐

GET_THE_TERMS与wp_GET_POST_TERMS中的奇怪结果

我正在尝试制作一个面包屑函数,但有一个小问题。。。使用时:$categories = get_the_terms( $post->ID, \'product_cat\' ); 我得到了一个循环中使用的类别数组,等等。唯一的问题是它是按字母顺序排列的。(我希望它按层次顺序排列。)经过一番挖掘,我发现了一种替代方法,即使用wp\\u get\\u post\\u terms(),但我肯定遗漏了一些东西,因为当我使用此方法时:$categories = wp_get_post_terms( $p