2017-04-22 41 views
0

我很難在wordpress中設置同位素佈局模式。我想要做的是創建一個2列網格,如this example所示。到目前爲止,我設法使同位素與過濾類別一起工作。我唯一要做的就是在例子中看到2列網格中的響應圖像。這是我到目前爲止有:live example同位素+ Wordpress佈局模式

的index.php

<div class="container-fluid"> 
<div class="container-moving"> 

    <div class="row"> 
    <div class="col-xs-12 col-sm-6"> 
<?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"); //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) ?> 
      <?php if (has_post_thumbnail()) { 
         the_post_thumbnail(); 
       } ?><br> 
       <?php the_title(); ?> 
    </div> <!-- end item --> 
    <?php endwhile; ?> 
    </div> <!-- end isotope-list --> 
<?php endif; ?> 
    </div> 

isotope.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('#filters'); 
$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; 
}); 

}); 

isotope.css

/* Start: Recommended Isotope styles */ 

/**** Isotope Filtering ****/ 

.isotope-item { 
    z-index: 2; 

} 

.item { 

} 


.isotope-hidden.isotope-item { 
    pointer-events: none; 
    z-index: 1; 
} 

/**** Isotope CSS3 transitions ****/ 

.isotope, 
.isotope .isotope-item { 
    -webkit-transition-duration: 0.8s; 
    -moz-transition-duration: 0.8s; 
     -ms-transition-duration: 0.8s; 
     -o-transition-duration: 0.8s; 
      transition-duration: 0.8s; 
} 

.isotope { 
    -webkit-transition-property: height, width; 
    -moz-transition-property: height, width; 
     -ms-transition-property: height, width; 
     -o-transition-property: height, width; 
      transition-property: height, width; 
} 

.isotope .isotope-item { 
    -webkit-transition-property: -webkit-transform, opacity; 
    -moz-transition-property: -moz-transform, opacity; 
     -ms-transition-property:  -ms-transform, opacity; 
     -o-transition-property:  -o-transform, opacity; 
      transition-property:   transform, opacity; 
} 

/**** disabling Isotope CSS3 transitions ****/ 

.isotope.no-transition, 
.isotope.no-transition .isotope-item, 
.isotope .isotope-item.no-transition { 
    -webkit-transition-duration: 0s; 
    -moz-transition-duration: 0s; 
     -ms-transition-duration: 0s; 
     -o-transition-duration: 0s; 
      transition-duration: 0s; 
} 
/* End: Recommended Isotope styles */ 

回答

0

首先要做的將是爲wordpress創建圖片大小,強制圖片具有相同的高度和寬度屬性。

add_image_size('masonryLayout', 300, 300, true); 

請注意,您需要重新上傳圖片才能在媒體庫中創建新的圖片大小。

然後你想要強制.item div是50%的寬度。 img標籤需要填充.item容器。

.item{ 
    width: 50% 
} 
.item img{ 
    width: 100%; 
    height: auto; 
} 
+0

我建議在嘗試合併到Wordpress之前創建佈局並使其在靜態HTML中完美。這種方式更容易。 –

+0

感謝您的建議,但是現在,當我在firefox或safari中加載我的頁面時,我看到我的圖像堆疊在一起。當我過濾它們或點擊所有圖像時,它已經消失 –

+0

我以前見過這種行爲。這是在圖像完全下載之前同位素激發時發生的錯誤。你可以通過使用圖片加載插件來解決這個問題。 https://github.com/desandro/imagesloaded –