Skip to content

Instantly share code, notes, and snippets.

@Kernix13
Last active July 26, 2023 15:18
Show Gist options
  • Save Kernix13/f919920ea84e71224dd67f8ac0ccd846 to your computer and use it in GitHub Desktop.
Save Kernix13/f919920ea84e71224dd67f8ac0ccd846 to your computer and use it in GitHub Desktop.
WordPress Recent Posts Custom Query
<!-- Add to a plugin or in front-page.php for a custom theme -->
<aside class="bgcolor4">
<div class="container">
<h3 class="custom-title"><?php esc_html_e('Latest Articles ', 'tower') ?></h3>
<div class="row">
<?php
$query = new WP_Query( [
'post__not_in' => get_option( 'sticky_posts'),
'post_type' => 'post',
// 'category_name' => 'code',
'posts_per_page' => 3,
] );
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post(); ?>
<div class="recent-row">
<?php the_post_thumbnail() ?>
<a class="recent-posts-link" href="<?php the_permalink(); ?>" rel="bookmark"><h4><?php the_title(); ?></h4></a>
<?php get_template_part( 'template-parts/content', 'author' ); ?>
<!-- <span><?php the_excerpt(); ?></span> -->
<div class="excerpt"><?php echo wp_trim_words(get_the_excerpt(), 28); ?></div>
<div class="recent-post-widget">
<a class="recent-posts-button" href="<?php the_permalink(); ?>">Read more...</a>
</div>
</div>
<?php
}
}
wp_reset_postdata();
?>
</div>
</div>
</aside>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment