I have the following code in template-home page which queries for a custom post type, called reviews. I have a Meta Box for the Custom Post Type where I can write the Author, Article Link, an excerpt to be displayed on the Home Page and a checkbox. If the checkbox is checked that post gets pulled on the Home Page.
<?php
if(have_posts()) {
while(have_posts()) {
the_post();
// store the page content for future use
$home_content = get_the_content();
if(has_post_thumbnail()) { ?>
<div class="rounded-image">
<?php the_post_thumbnail(); ?>
</div>
<?php }
}
}
// RESET ORIGINAL QUERY POST DATA
wp_reset_postdata();
?>
<?php
// START THE SECOND QUERY TO RETRIEVE THE REVIEWS
$args = array('post_type' => 'reviews');
$home_reviews_query = new WP_Query($args);
if($home_reviews_query->have_posts()) {
while($home_reviews_query->have_posts()) {
$home_reviews_query->the_post();
// Retrieve Meta Box Options
$checked_review = get_post_meta($post->ID, 'review_check', true);
$review_author = esc_html(get_post_meta($post->ID, 'review_author', true));
$review_excerpt = esc_html(get_post_meta($post->ID, 'review_excerpt', true));
$review_content = get_the_content();
if($checked_review && $review_content != '') { ?>
<blockquote>
<?php if($review_excerpt != '') : ?>
<p>
<?php
echo $review_excerpt;
?>
</p>
<?php elseif($review_excerpt == '') : ?>
<p>
<?php
the_excerpt();
?>
</p>
<?php endif; ?>
<cite>- <?php echo $review_author; ?> -</cite>
</blockquote>
<?php }
}// end while
} // end if
// RESET SECOND QUERY POSTDATA
wp_reset_postdata();
?>
I would like that if there aren't any posts checked, I could display something else instead of the reviews, like the_content() for the Home Page, as backup in case someone forgets to check any reviews. I was thinking I could check if $checked_review is false for all posts and if that returned true it would then display some other bit of content. However I have no idea to properly do that.
I tried the following piece of code in the loop and after the loop but with no result.
if($checked_review == false) {
echo $home_content;
}
I previously stored the Home Page the_content() in the $home_content variable. before I started the query for the Reviews Post Type.
Had a go with get_posts() as well but to no solution. Hopefully my post is not too confusing.
I'm not that good with PHP so any suggestions/push in the right direction would be greatly appreciated.