所以我知道单列中的砌体已经在堆栈上被覆盖了几次,但我对jquery不是很熟悉,我不确定我需要做出的调整。我也不是非常精通wordpress,不知道我是否在这里犯了一个明显的错误。我正在编辑一个主题,我正在尝试使博客使用砖石布局。 该主题从它自己的 php 文件调用 post 循环,因此博客被分解为几个 php 文件。 我希望我包含正确的信息。
这些帖子以块的形式显示,但它只是直接向下的一列。 似乎容器在每个帖子的页面上一直走过。 我不确定它是否没有停止循环,或者我需要添加什么,以便每个帖子都分布在容器宽度上。 任何关于我做错了什么的帮助或提示将不胜感激。 提前谢谢。
我将其添加到我的函数中。
function wdv_enqueue_scripts() {
wp_enqueue_script( 'jquery-masonry' ); // adds masonry to the theme
}
add_action( 'wp_enqueue_scripts', 'wdv_enqueue_scripts' );
这是我的页脚.php
<script>
jQuery( document ).ready( function( $ ) {
$( '#container2' ).masonry( { columnWidth: 220 } );
} );
</script>
这是我的循环代码。
<div id="container2">
<?php
global $ae_post_factory;
$ae_post = $ae_post_factory->get('post');
$post = $ae_post->current_post;
?>
<div class="brick">
<div class="brick_header">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" >
<h4 class="media-heading title-blog"><?php the_title(); ?></h4>
</a>
</div>
<div class="brick_featured_image">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail (); ?>
</a>
<?php endif; ?>
</div>
<?php the_excerpt(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" class="read_more_link">Read More</a>
</div>
</div><!-- container -->
这就是 CSS
* masonry brick layout */
#container2 {
width: 100%; /* width of the entire container for the wall */
margin-bottom: 10px;
}
.brick {
width: 30%; /* width of each brick less the padding inbetween */
padding: 0px 10px 15px 10px;
background-color: #fff;
margin-top: 5px;
}
.brick_header{
border-bottom: solid 1px #1d1d1d;
padding-bottom: 10px;
padding-top: 10px;
}
.brick_header a{
color: #ffff00;
}
.brick_header a:hover{
color: white;
}
.brick_featured_image{
width: 100%;
margin-top: 10px;
}
.brick_featured_image img{
width: 100%;
height: auto;
}
在这里,您以 JS 220px 格式提供列宽。而在 CSS 中:30%。这可能会产生问题。
确保您的 HTML 结构如下所示。
<div id="container2">
<div class="brick">...</div>
<div class="brick">...</div>
<div class="brick">...</div>
</div>
和 CSS :
.container2{
width:100%;
}
.brick{
width:25%;
/*
This should be respective of the columns you want
Like for 4 columns, 100%/4 = 25%
*/
}
和JS。
var $container2 = $('#container2');
$container.masonry({
itemSelector: '.brick'
});
有关更详细的解释:http://masonry.desandro.com/#getting-started
要了解有关您遇到的问题的更多详细信息,请提供URL,以便我可以查看并提供准确的解决方案。
我个人使用"js-masonry"类主要是因为如果您使用的是Bootstrap或Modest Grid之类的框架,它将保留装订线设置等。
下面是一个示例:
<div class="js-masonry">
<?php if ( have_posts() ): ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="dt-6 tl-6 tp-6">
<article>
<h2>
<a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark">
<i class="fa <?php echo strtolower(str_replace(" ", "-", get_field('project_type'))); ?>"></i>
<?php the_title(); ?>
</a>
</h2>
<?php the_excerpt(); ?>
<div class="download">
<a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark">
View <?php the_title(); ?>
</a>
</div>
</article>
</div>
<?php endwhile; ?>
<?php else: ?>
<h2>No posts to display</h2>
<?php endif; ?>
请注意,<div class="js-masonry">
在 while 之外。