Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Steve Smith
Steve Smith
8,789 Points

Else Statement not working ... How to Build a WordPress Theme

Not a big deal but driving me a little insane. The Else Statements throughout this project don't seem to work. When there is content to display then it displays it just fine ... but when testing the Else part then nothing is display even though it should throw out a 'Nothing to display' message. Have been through the code and all seems fine and have also copied the code from the Project Files and still doesn't work. Below is the code for my work page. Any help would be much appreciated ... as would any suggestions regarding a possible non-code based cause. Many thanks, Steve

<?php 

/*

    Template Name: Work Page

*/


get_header(); ?>

<div class="grid_12 omega clearfix">

<?php

$args = array(
    'post_type' => 'work'
);

$the_query = new WP_Query( $args );

?>


<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="grid_6 spotlight project" style="background-color: <?php the_field( 'background_color' ); ?>">

    <a href="<?php the_permalink(); ?>">
        <img src="<?php the_field( 'homepage_slider_image' ); ?>">
    </a>

    <h4>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </h4>


    <?php the_field( 'description' ); ?>

    <p>
        <a class="btn blue" href="<?php the_permalink(); ?>" style="background-color: <?php the_field( 'button_color' ); ?>">
            View Project &rarr;
        </a>
        </p>

    </div>

<?php endwhile; else: ?>

    <p>There are no posts or pages here</p>

<?php endif; ?>

</div>

<?php get_footer(); ?>

2 Answers

The problem is with your if statement. You are doing a custom query on post _type work your if statement is

if(have_posts())

Which means if there are any posts in wordpress Replace your if ststement with

if($the_query->have_posts()):
    //---

    //--
 else:
    echo "No Work Done";
 endif;

to check that there are posts related to post type work. This will solve the problem.

Steve Smith
Steve Smith
8,789 Points

Hey Abhay, That worked a treat ... thank you very much for putting me out of my misery! Kind regards, Steve

Steve Smith
Steve Smith
8,789 Points

That worked a treat Abhay.

However, still not sure why in the project files it uses

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

and there is even a related quiz question where this is the correct answer.

Am I missing something or is it just a mistake in the course?

Thanks,

Steve