同位素过滤自定义样式问题

Isotope filtering custom styling issue

我需要像下图一样设置同位素过滤器的样式。

同位素过滤是否可行jQuery?

注意:- 我已经在我的 WordPress 站点中实现了这个同位素过滤。所有过滤器都是自定义 post 类型类别,右侧内容(过滤器 1 内容 A,过滤器 1 内容 B 等)是 post 类型标题,左侧内容是自定义 post输入内容。

请按照以下步骤操作:

1.create 一个模板将其命名为 template-isotope-grid.php 并将此代码放入其中。

<?php
/**
 * The template for displaying all posts in isotope.
 *
 * Template Name: Isotope Grid View
 *
 * @package storefront
 */

get_header(); ?>
<?php
    $query = new WP_Query(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1
));
$categories = get_categories();
//print_r($categories);
?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
        <?php if ($query->have_posts()): ?>
            <div id="filter-menu" class="filter-button-group">              
              <button data-filter="*" class="reset active row-btn">All</button>
              <?php foreach($categories as $category):?>
              <button data-filter=".<?php echo $category->slug;?>" class="row-btn"><?php echo $category->name; ?></button>
              <?php endforeach; ?>
            </div>
        <div class="row">
        <div class="col-md-8 col-xs-8 col-sm-8" id="content_section">
        </div>


        <div class="col-md-4 col-xs-4 col-sm-4">
            <div id="grid">
            <?php while ( $query->have_posts() ) : $query->the_post();
                $categories = get_the_category(get_the_ID());
                $cat_str = '';
                foreach ($categories as $category) {
                    $cat_str .=$cat_str.' '.$category->slug;
                }

                echo '<div class="content-item '.$cat_str.'" style="display:none;">';
                echo '<h2>'.get_the_title().'</h2>';
                if (has_post_thumbnail( get_the_ID() ) ):
                $image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'single-post-thumbnail' );
                echo '<div class="img_class"><img src="'.$image[0].'" /></div>';
                endif;
                echo '<div class="post_content">'.get_the_content().'</div>';
                echo '</div>';

                echo '<div class="item '.$cat_str.'">'.get_the_title().'</div>';              

            endwhile; // End of the loop. ?>
            </div>
        </div>
        </div>
<?php wp_reset_query();
     else:
      // No, we don't have any posts, so maybe we display a nice message
      echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
     endif;
?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php
get_footer();
  1. 从管理员创建一个页面,然后从右侧选择您创建的模板Isotope Grid View。请参见屏幕截图。

  2. 将此 css 放入 style.css 文件中:

button {
  cursor: pointer;
  border: none;
  background: none;
}
#filter-menu {
  padding: 20px 0;
  text-align: center;
}
button:focus {
  outline: 0;
}

button.active {
  background: grey;
}

#grid {
  width: 100%;
}

#grid .item { 
     position: unset !important;
    left: unset !important;
    top: unset !important;
}
.item.selected {
    font-weight: bold;
}

  1. 包含 thic boostrap 文件 header.php 文件:

    https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css

  2. 将这些 js 文件包含在 footer.php 文件中:

    https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/2.2.2/isotope.pkgd.min.js https://cdn.jsdelivr.net/isotope.packery/1.3.2/packery-mode.pkgd.min.js

并将脚本放入 footer.php 文件:

<script type="text/javascript">

    var $grid = jQuery('#grid').isotope({
      itemSelector: '.item',
      layoutMode: 'packery',
    });

    jQuery(function() {
      var $buttons = jQuery('#filter-menu button');
      $buttons.on('click', function() {
        var filterValue = jQuery(this).attr('data-filter');
        jQuery(this).addClass('active').siblings().removeClass('active');
        $grid.isotope({
          filter: filterValue
        });
      });
    });

    jQuery(window).on('load',function(){
        jQuery('#grid').find('.item').first().addClass('selected');
        var firstElement = jQuery('#grid').find('.item.selected').prev().html();
        console.log('Load: ',firstElement);
        jQuery('#content_section').html(firstElement);
    });
    jQuery('button.row-btn').on('click',function(){
        setTimeout(function(){
            jQuery('#grid').find('.item').removeClass('selected');
        jQuery('#grid').find('.item:visible').first().addClass('selected');
        var firstElement = jQuery('#grid').find('.item.selected').prev().html();
        console.log('Load: ',firstElement);
        jQuery('#content_section').html(firstElement);
    },1000);
    });
        jQuery(document).on('click','div.item',function(){  
            console.log('Load: ',firstElement);
            jQuery('#grid').find('.item').removeClass('selected');
            jQuery(this).addClass('selected');
            var firstElement = jQuery(this).prev().html();

            jQuery('#content_section').html(firstElement);

    });
    </script>

截图: