我需要对同位素过滤器进行样式设置,如下图所示。
同位素过滤jQuery是否可行?
注意:-,我已经在我的WordPress网站中实现了这种同位素过滤。所有过滤器均为自定义帖子类型类别,右侧内容(过滤器1内容A,过滤器1内容B等)为帖子类型标题,左侧内容为自定义帖子类型内容。
答案 0 :(得分:1)
请按照以下步骤操作:
1。创建一个名为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();
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;
}
包括thic boostrap文件header.php文件:
https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
将这些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>
屏幕截图: