dpaal28266 2018-12-10 14:50
浏览 44
已采纳

将isotope.js与Wordpress循环集成

I've been trying to get isotope.js working on a Wordpress site. I've been following this tutorial https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/ and have been able to get it all functioning. For my design, I'm trying to add <div class="grid-sizer"></div> every four posts that are called. I've been referring to this question: Wrap every 4 posts in a custom wordpress loop with a div but cannot seem to figure out the proper placement for the count and i statements. Can anyone help me figure this out? Here's my loop right now:

<?php 
                 $terms = get_terms( array(
                     'taxonomy' => 'solutions',
                     'parent' => 0
                    )
                 ); // get all categories, but you can use any taxonomy
                 $count = count($terms); //How many are they?
                 if ( $count > 0 ){  //If there are more than 0 terms
                 foreach ( $terms as $term ) {  //for each term:
                 echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>
";
                 //create a list item with the current term slug for sorting, and name for label
                 }
                 } 
            ?>
        </ul>
        <?php 
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => asc,
            'tax_query' => array(
                array(
                    'taxonomy' => 'solutions',
                    'field' => 'term_id',
                    'terms' => get_queried_object()->term_id,
                ),
            ) );
        $the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
        <?php if ( $the_query->have_posts() ) : ?>
        <div id="isotope-list">
            <?php 

             while ( $the_query->have_posts() ) : $the_query->the_post(); 
             $termsArray = get_the_terms( $post->ID, "solutions" );  //Get the terms for this particular item
             $termsString = ""; //initialize the string that will contain the terms
            $i = 0;
             foreach ( $termsArray as $term ) { // for each term 
             $termsString .= $term->slug.' '; //create a string that has all the slugs                   
             }
                if($i%4 == 0) {
                    echo "<div class='grid-sizer'> </div>"; 
                }
            ?>
            <div class="<?php echo $termsString; ?>item">
                <p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
                <h4 class="product-name">
                    <?php the_title(); ?>
                </h4>
            </div>
            <?php $i++; ?>
            <!-- end item -->
            <?php endwhile;  ?>
        </div>
        <!-- end isotope-list -->
        <?php endif; ?>
  • 写回答

1条回答 默认 最新

  • drob50257447 2018-12-18 14:45
    关注

    Here's the resulting code - thanks for misorude's help!

    <?php 
                 $terms = get_terms( array(
                     'taxonomy' => 'solutions',
                     'parent' => 0
                    )
                 ); // get all categories, but you can use any taxonomy
                 $count = count($terms); //How many are they?
                 if ( $count > 0 ){  //If there are more than 0 terms
                 foreach ( $terms as $term ) {  //for each term:
                 echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>
    ";
                 //create a list item with the current term slug for sorting, and name for label
                 }
                 } 
            ?>
        </ul>
        <?php 
        $i = 0;
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order' => asc,
            'tax_query' => array(
                array(
                    'taxonomy' => 'solutions',
                    'field' => 'term_id',
                    'terms' => get_queried_object()->term_id,
                ),
            ) );
        $the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
        <?php if ( $the_query->have_posts() ) : ?>
        <div id="isotope-list">
            <?php 
    
             while ( $the_query->have_posts() ) : $the_query->the_post(); 
             $termsArray = get_the_terms( $post->ID, "solutions" );  //Get the terms for this particular item
             $termsString = ""; //initialize the string that will contain the terms
             foreach ( $termsArray as $term ) { // for each term 
             $termsString .= $term->slug.' '; //create a string that has all the slugs                   
             }
                if($i%4 == 0) {
                    echo "<div class='grid-sizer'> </div>"; 
                }
            ?>
            <div class="<?php echo $termsString; ?>item">
                <p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
                <h4 class="product-name">
                    <?php the_title(); ?>
                </h4>
            </div>
            <?php $i++; ?>
            <!-- end item -->
            <?php endwhile;  ?>
        </div>
        <!-- end isotope-list -->
        <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备