• Resolved Vince_M

    (@vince_m)


    I have a page (careers) that I use with a custom loop to display any positions coming up. One problem I am having is if there is no positions I would like it to display:

    “There are no job openings at this time.”

    For some reason I can’t get the default text to display if there are no positions.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();  ?>
    <?php the_content();  ?>
    <?php endwhile; endif; ?>
    <hr>
    <?php
       p_reset_postdata();
       $args = array(
       'category_name' => 'careers'
        );
       $the_query = new WP_Query( $args );
       //wp_reset_postdata();
    ?>
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <h3><a href="<?php the_permalink(); ?>"><?php the_title() ;?></a></h3>
    <?php the_excerpt(); ?>
    <hr>
    
    <?php endwhile; else: ?>
    <p>There are no job openings at this time.</p>
    <?php endif; ?>

    Here is the page in question:


    Careers page in question

    Any help would be greatly appreciated.

    Vince

Viewing 2 replies - 1 through 2 (of 2 total)
  • You missed your query on the “if(have_posts())” in your custom query — so it’s querying every single post in the wordpress database to see “if” there are any (which of course there are), and then since you have the while loop only querying “have_posts” on the custom arguments, it’s only showing posts within that.

    Essentially you’re asking it:
    “If WordPress has posts”
    “Show only these types of posts”

    when you want to ask it:
    “If WordPress has these specific types of posts”
    “Show only these types of posts”

    Once you correct that logic, it should respond to the “else” correctly.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();  ?>
    <?php the_content();  ?>
    <?php endwhile; endif; ?>
    <hr>
    <?php
       	wp_reset_postdata();
       	$args = array(
       		'category_name' => 'careers'
        );
       	$the_query = new WP_Query( $args );
    	if ( $the_query->have_posts() ) :
    		while ( $the_query->have_posts() ) :
    			$the_query->the_post();
    			?>
    				<h3>
    					<a href="<?php the_permalink(); ?>">
    						<?php the_title() ;?>
    					</a>
    				</h3>
    				<?php the_excerpt(); ?>
    				<hr>
    			<?php
    		endwhile;
    	else:
    		?>
    		<p>There are no job openings at this time.</p>
    		<?php
    	endif;
    ?>

    Also one big thing that really expedites learning — keep your code organized. For example, my system involves nesting with the tab key. It helps me keep things in line so I can quickly follow the code down the page and see where I messed up. No offense, but your original code was a bit scrambled. I feel you may have been able to see the error, if it was more organized. You don’t have to follow the nesting organization like I did (although a lot of developers do), but figure something out, because it also helps you focus on the code more, and you’ll be able to spot mistakes more easily.

    Best of luck to you, Vince. If you have any questions about the above code, or if it turns out that it doesn’t work, let me know.

    Thread Starter Vince_M

    (@vince_m)

    Thank you, Endlyss. I followed your code and see where I did go wrong. As for my code I have followed how I learned, but I see where you are coming from. Thanks and have fixed my code.

    Vince

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom loop default text if no posts’ is closed to new replies.