[php]
<div class="row">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-12 col-md-6 col-lg-4">
<div class="weekly-news">
<article <?php post_class(); ?>>
<div class="entry-thumbnail">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>
</a>
<?php else : ?>
<!-- Default image -->
<?php
// Get the URL to the theme directory
$theme_directory = get_template_directory_uri();
// Construct the URL to the screenshot.png
$image_url = $theme_directory . '/screenshot.png';
?>
<img src="<?php echo esc_url($image_url); ?>" alt="Screenshot" class="img-fluid">
<?php endif; ?>
</div>
<div class="entry-content">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<!-- <?php the_content(); ?> -->
</div>
</article>
</div>
</div>
<?php endwhile; ?>
<?php the_posts_pagination(); ?>
<?php else : ?>
<p><?php esc_html_e('No posts found.', 'rubinastheme'); ?></p>
<?php endif; ?>
</div>
[/php]Let’s break it down step by step:
- Opening
<div class="row">: This indicates the start of a row in a grid system. It uses a CSS framework Bootstrap. <?php if (have_posts()) : ?>: This checks if there are any posts available to display.<?php while (have_posts()) : the_post(); ?>: This initiates a loop through each available post.<div class="col-12 col-md-6 col-lg-4">: Within each iteration of the loop, this div represents a column in the grid. The classescol-12,col-md-6, andcol-lg-4define the column widths for different screen sizes using Bootstrap’s grid system.<article <?php post_class(); ?>>: This creates an HTML article element with dynamic classes based on WordPress’s post type and other attributes.<?php if (has_post_thumbnail()) : ?>: This checks if the current post has a featured image.<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">: This creates a link to the post’s permalink (URL) and sets the title attribute to the post’s title.<?php the_post_thumbnail('thumbnail'); ?>: This outputs the post’s featured image with the ‘thumbnail’ size.<?php else : ?>: If the post doesn’t have a featured image, this part of the code is executed.<img src="<?php echo esc_url($image_url); ?>" alt="Screenshot" class="img-fluid">: This outputs the default image with the constructed URL.<?php endif; ?>: Ends the conditional statement for checking if the post has a featured image.<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>: This outputs the post’s title as a link to the post’s permalink.<?php the_content(); ?>: This would normally output the post’s content, but it’s currently commented out.<?php endwhile; ?>: Ends the loop through each post.<?php the_posts_pagination(); ?>: This displays pagination links for navigating between pages of posts.<?php else : ?>: If there are no posts found, this part of the code is executed.<p><?php esc_html_e('No posts found.', 'rubinastheme'); ?></p>: This outputs a message indicating that no posts were found.<?php endif; ?>: Ends the conditional statement for checking if there are any posts to display.
Overall, this code creates a responsive grid layout for displaying posts, with each post represented by a thumbnail image, title, and optionally, content. If no posts are found, it displays a message indicating so.