0

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.

1 Answer 1

0

It isn't clear how you are getting $home_content but you will need two loops: one to get the home page content, however you are retrieving that, and one for your reviews content above. You will need to use wp_reset_postdata() after both loops.

6
  • after the get_header() I'm just doing a normal loop where I also make use of the post_thumbnail() and i'm also storing the page content into that variable. I'm using reset_post_data() before the second query.
    – aroundsix
    Sep 23, 2015 at 21:55
  • Updated the first post to make it more clear. Apologize I didn't make it clear the first time.
    – aroundsix
    Sep 23, 2015 at 22:07
  • Is $home_content empty? If not, can you just do: if($checked_review && $review_content != '') { // Output reviews } else { echo $home_content; } Sep 24, 2015 at 8:55
  • The $home_content has some lorem ipsum. I had already tried what you suggested above but that outputs the $home_content for every other review that isn't checked. Also tried 'if($checked_review)' outside the loop and if there's even one review that isn't checked it outputs the $home_content regardless if there are any checked. I need a way to check all the reviews for that meta box value and if all of them are unchecked then display the $home_content just once. I feel like there's probably an easy way to do this and I'm just missing it.
    – aroundsix
    Sep 24, 2015 at 12:15
  • What I'm not clear about is your set-up. You say you are using a custom template for this but what posts is the first loop pulling in? I'd suggest creating a home.php template and adjusting your first loop to pull in a post from a category called 'Home page'. Here is an example of something similar I use: pastebin.com/1HzbN0SB You probably need $checked_review to be an array $checked_review[] = get_post_meta($post->ID, 'review_check', true); so that it isn't replaced each time in the loop. Then out of the loop check if (sizeof($checked_review) > 0) { ... Sep 24, 2015 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.