同位素+ get_theme_mod类别未过滤

时间:2018-10-21 16:56:36

标签: wordpress wordpress-theming isotope

早上好,我关注了艾丽西娅·拉米雷斯(Alicia Ramirez)的同位素文章

https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/

我要实现的功能与文章相似,但不是使用预定义的类别,而是将定制器/ get_theme_mode(投资组合类别)用作类别过滤器。 该类别正在运行,并且正在显示所有类别中的所有帖子,但是,类别链接未传递到类别列表<li>links</li>

下面是Alicia Ramizez的代码

     <div id="test" class="sectionpadding bg-warning">
        <ul id="filters">
          <li><a href="#" data-filter="*">Everything</a></li>
                 <?php
                 $categories=get_categories(
                   array( 'parent' => $cat->cat_ID,
                  'parent' => get_theme_mod('portfolio_category'),
                  ));
                  var_dump($categories);
                 $terms = get_terms('category', array('parent' =>  
        get_theme_mod('portfolio_category'))); // you can use any taxonomy, 
       instead of just 'category'
                 $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>\n";                         //create a list item with the current term slug for sorting, and name for label
                                                    }
                     }
                 ?>
        </ul>

  <?php
     $terms_ID_array = array();
     foreach ($terms as $term)
     {
         $terms_ID_array[] = $term->term_id; // Add each term's ID to an array
     }
     $terms_ID_string = implode(',', $terms_ID_array); // Create a string with all the IDs, separated by commas
     $the_query = new WP_Query( '
     posts_per_page=50&cat='.$terms_ID_string ); // Display 50 posts that belong to the categories in the string ?>

     <?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, "category" );  //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
      }
      ?>
      <div class="<?php echo $termsString; ?> item"> <?php // 'item' is used as an identifier (see Setp 5, line 6) ?>
        <h3><?php the_title(); ?></h3>
         <?php
              if ( has_post_thumbnail() ) {
                      the_post_thumbnail();
                } ?>
                </div> <!-- end item -->
              <?php endwhile;  ?>
            </div> <!-- end isotope-list -->
<?php endif; ?>
</div>


my jquery enqueue script

        function add_isotope() {
    wp_register_script( 'isotope', get_template_directory_uri().'/inc/asssets/js/jquery.isotope.js', array('jquery'),  true );
    wp_register_script( 'isotope-init', get_template_directory_uri().'/inc/asssets/js/isotope.js', array('jquery', 'isotope'),  true );
    wp_register_style( 'isotope-css', get_stylesheet_directory_uri() . '/inc/asssets/css/isotope.css' );

    wp_enqueue_script('isotope-init');
    wp_enqueue_style('isotope-css');
}

add_action( 'wp_enqueue_scripts', 'add_isotope' );

谢谢

0 个答案:

没有答案