我需要创建包含最新帖子的砖石网格。我创建了一个基于https://masonry.desandro.com/ - 栅格显示正确。
我请求帮助创建一个wp循环,该循环将在网格中显示帖子。
这是我的网格:
<!-- grid masonry layout -->
<div class="news-wrapper container">
<div class="grid">
<div class="grid-sizer"></div>
<div class="gutter-sizer"></div>
<div class="grid-item grid-item--width2"></div>
<div class="grid-item grid-item--height2"></div>
<div class="grid-item grid-item--width2"></div>
<div class="grid-item"></div>
<div class="grid-item grid-item--width2"></div>
<div class="grid-item grid-item--width2"></div>
<div class="grid-item grid-item--height2"></div>
<div class="grid-item grid-item--height2"></div>
<div class="grid-item grid-item--width2"></div>
<div class="grid-item grid-item--width2"></div>
</div>
</div>
单个网格元素应包含单个帖子(帖子类别名称、标题、描述、阅读更多按钮)。从包含日期起,应显示所选类别中的帖子。
我创建了以下代码:
<?php
$args = array( array( \'category__and\' => array( 55, 61, 53, 59, 57 ) ), \'post_status\' => \'publish\', \'orderby\' => \'ASC\' );
$news_query = null;
$news_query = new WP_Query( $args );
?>
<!-- grid masonry layout -->
<div class="news-wrapper container">
<div class="grid">
<?php if ($news_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($news_query->have_posts()) : $news_query->the_post(); ?>
<?php $count++; ?>
<div class="grid-sizer"></div>
<div class="gutter-sizer"></div>
<?php if ( $count == 1 ) : ?>
<div class="grid-item grid-item--width2">
<div class="card card-media">
<a href="#">
<div class="news-cat"><?php the_category(\', \'); ?></div>
</a>
<div class="card-body">
<h5 class="card-title"><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h5>
<p class="card-text"><?php the_excerpt(); ?></p>
<a href="#" class="btn"><span class="nav-text">Read more</span><i class="fa-xs fas fa-arrow-right"></i></a>
</div>
</div>
</div>
<?php elseif ($count == 2) : ?>
<div class="grid-item grid-item--height2"> (..and next grid item...)
我引入了计数,因为网格元素有不同的样式(有时是双倍宽度、双倍高度),所以我无法在循环中创建相同的网格元素,所以如何更改计数以显示所有帖子(无论其数量)?
SO网友:sunnyluk
如前所述,我创建了一个循环。
<div class="grid">
<div class="grid-sizer"></div>
<div class="gutter-sizer"></div>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div id="grid-item-nr-<?php echo $post_nr; ?>" <?php post_class( \'grid-item\' ); ?>>
<div class="card">
<div class="news-cat"><?php the_category(\', \'); ?></div>
<div class="card-body">
<h5 class="card-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<p class="card-text">
<?php if ( is_category() || is_archive() ) {
echo excerpt(20);
} else {
echo excerpt(20);
} ?>
</p>
<a href="<?php the_permalink(); ?>" class="btn"><span class="nav-text">Czytaj więcej</span><i class="fa-xs fas fa-arrow-right"></i></a>
</div>
</div>
</div>
<?php $post_nr++; endwhile; ?>
<!-- end of the loop -->
</div>
然后我创建了一个脚本(我当然可以优化…)将类添加到相应的框中,每隔11个元素重复一次。
$(function() {
var item = 0;
var a = 0; // 1 element
var b = 3; // 4 element
var c = 5; // 6 element
var d = 6; // 7 element
var e = 9; // 10 element
var f = 10; // 11 element
var g = 1; // 2 element
var h = 7; // 8 element
var i = 8;; // 9 element
var j = 2; // 3 element
var k = 4; // 5 element
for (item = 0; item < 100; item++) {
$(\'.grid\').find("div.grid-item:eq(" + a + ")").addClass(\'grid-item--width2\');
$(\'.grid\').find("div.grid-item:eq(" + b + ")").addClass(\'grid-item--width2\');
$(\'.grid\').find("div.grid-item:eq(" + c + ")").addClass(\'grid-item--width2\');
$(\'.grid\').find("div.grid-item:eq(" + d + ")").addClass(\'grid-item--width2\');
$(\'.grid\').find("div.grid-item:eq(" + e + ")").addClass(\'grid-item--width2\');
$(\'.grid\').find("div.grid-item:eq(" + f + ")").addClass(\'grid-item--width2\');
$(\'.grid\').find("div.grid-item:eq(" + g + ")").addClass(\'grid-item--height2\');
$(\'.grid\').find("div.grid-item:eq(" + h + ")").addClass(\'grid-item--height2\');
$(\'.grid\').find("div.grid-item:eq(" + i + ")").addClass(\'grid-item--height2\');
$(\'.grid\').find("div.grid-item:eq(" + j + ") p.card-text").hide();
$(\'.grid\').find("div.grid-item:eq(" + k + ") p.card-text").hide();
a += 11;
b += 11;
c += 11;
d += 11;
e += 11;
f += 11;
g += 11;
h += 11;
i += 11;
j += 11;
};
});
类是在调用砌体脚本之前添加的。取得了一定的效果。