使用同位素过滤器保持POST格式类

时间:2014-07-22 作者:user56920

当我为显示帖子添加同位素过滤时,我丢失了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;
});

});
感谢您的指导!

1 个回复
最合适的回答,由SO网友:Michael 整理而成

您没有描述您为添加同位素过滤而编辑的内容,但您似乎已删除post_class() 从此行添加同位素类:

<article class="<?php echo $termsString; ?>item"> 
将其更改为:

<article <?php post_class( $termstring . \'item\' ); ?>"> 

http://codex.wordpress.org/Function_Reference/post_class

结束

相关推荐

The Loop in Static Page

我对环路有一些问题。我以“Twenty14”主题为例。我正在使用基本循环创建2个php文件。一个是家。其中一个是名为示例页的模板页。php。两者都包含此代码;if( have_posts() ) : while( have_posts() ) : the_post(); the_content; endwhile; endif; 没什么特别的,唯一的区别是我在示例页面上有模板声明。php/** * Template Nam