Skip to content

Commit

Permalink
Show latest blog posts on search results page.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbohacek committed Nov 18, 2020
1 parent f27b126 commit 28b7a87
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
12 changes: 7 additions & 5 deletions loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<div class="col-sm-12">
<article>
<h2><?php _e( 'Sorry, nothing to display.', 'botwiki' ); ?></h2>
</article>
</div>
<?php if ( !is_search() ){ ?>
<div class="col-sm-12">
<article>
<h2><?php _e( 'Sorry, nothing to display?', 'botwiki' ); ?></h2>
</article>
</div>
<?php } ?>
<!-- /article -->
<?php endif; ?>
</div>
Expand Down
47 changes: 46 additions & 1 deletion search.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@



if ($wp_query->found_posts > 0){ ?>
if ($wp_query->found_posts > 0){ ?>
<h3>Not quite what you're looking for?</h3>
<?php }?>
<div class="container">
Expand All @@ -72,6 +72,51 @@
</li>
</ul>
</div>


<div class="container">

<h3 id="blog">Latest from the blog</h3>
<div id="blog-latest-wrapper" class="row list">
<?php
remove_all_filters( 'pre_get_posts' );

$latest_blog_posts = get_posts( array(
'posts_per_page' => 4,
'post_type' => 'post',
'post_status' => 'publish'
) );

foreach ( $latest_blog_posts as $blog_post ) {
$post_thumbnail_url = get_the_post_thumbnail_url( $blog_post->ID, 'medium' );
$post_thumbnail_url_full = get_the_post_thumbnail_url( $blog_post->ID );
$dominant_color = get_post_meta( $blog_post->ID, 'dominant_color', true );
$dominant_color_css = str_replace('[', 'background-color:rgb(', $dominant_color);
$dominant_color_css = str_replace(']', ')', $dominant_color_css);


?>
<div class="col-sm-6 col-md-6 col-lg-3 list-item">
<div class="card w-100">
<a href="<?php echo get_permalink( $blog_post->ID ); ?>">
<div class="overflow-hidden" style="<?php echo $dominant_color_css; ?>">
<img loading="lazy" class="lazy-load card-img-top" src="<?php echo $post_thumbnail_url; ?>" data-src="<?php echo $post_thumbnail_url_full; ?>" alt="<?php echo $blog_post->post_title; ?>">
</div>
</a>
<div class="card-body">
<h5 class="card-title">
<a href="<?php echo get_permalink( $blog_post->ID ); ?>"><?php echo $blog_post->post_title; ?></a>
</h5>
<p class="card-text"><?php echo $blog_post->post_excerpt; ?></p>
</div>
</div>
</div>
<?php } ?>
</div>
<p><a class="btn" href="/blog/">Visit the blog</a></p>
<!-- /post details -->
</div>

</div>
</div>
</main>
Expand Down

0 comments on commit 28b7a87

Please sign in to comment.