当我为显示帖子添加同位素过滤时,我丢失了format类。
无同位素过滤:
<article id="post-22" class="post-22 post type-post status-publish format-image hentry category-burma">
使用同位素过滤:
<article class="burma item isotope-item" style="position: absolute; left: 0px; top: 0px; transform: translate3d(0px, 56px, 0px);">
我需要保留format类(如上面示例中的“format image”)。
下面是我使用的代码:
<ul id="filters">
<li><a href="#" data-filter="*">All</a></li>
<?php
$terms = get_terms("category"); // 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>\\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php $the_query = new WP_Query( \'posts_per_page=50\' );
//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, "category", "format-name"); //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
}
?>
<article class="<?php echo $termsString; ?>item">
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
</article> <!-- end item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
<?php endif; ?>
和JS:
jQuery(function ($) {
var $container = $(\'#isotope-list\'); //The ID for the list with all the blog posts
$container.isotope({ //Isotope options, \'item\' matches the class in the PHP
itemSelector : \'.item\',
layoutMode : \'masonry\'
});
//Add the class selected to the item that is clicked, and remove from the others
var $optionSets = $(\'#filters\'),
$optionLinks = $optionSets.find(\'a\');
$optionLinks.click(function(){
var $this = $(this);
// don\'t proceed if already selected
if ( $this.hasClass(\'selected\') ) {
return false;
}
var $optionSet = $this.parents(\'.option-set\');
$optionSets.find(\'.selected\').removeClass(\'selected\');
$this.addClass(\'selected\');
//When an item is clicked, sort the items.
var selector = $(this).attr(\'data-filter\');
$container.isotope({ filter: selector });
return false;
});
});
感谢您的指导!