Add wp posts to cutom masonry grid
I need to create masonry grid containing the latest posts. I created a grid based on https://masonry.desandro.com/ - the grid displays correctly.
I am asking for help in creating a wp loop that will display posts in the grid.
This is my grid:
!-- 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
A single grid element should contain a single post (name of the post category, title, description, read more button). Posts should be displayed from the date of inclusion, from selected categories.
I created this code:
?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/spani 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...)
I have introduced counting, because the grid elements have different styling (sometimes double width, double height), so I can not create identical grid elements in the loop, so how to change counting to display all posts (regardless of their number)?