• Resolved hoffcomm

    (@hoffcomm)


    I have some code that spits out posts if it is in a certain category, if there are no posts in that category then I want it to spit out text that says “There are no Workshops at this time.”

    <?php
    $catquery = new WP_Query( 'cat=2&posts_per_page=100&order=ASC' );
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <b><a href="https://artspacelansdowne.com/workshops/#<?php the_ID(); ?>"><?php the_title(); ?></a></b><?php the_meta(); ?>
    
    <?php endwhile; ?>

    My website is https://artspacelansdowne.com/

Viewing 5 replies - 1 through 5 (of 5 total)
  • $catquery = new WP_Query( 'cat=2&posts_per_page=100&order=ASC' );
    
    If have_posts {
    
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <b><a href="https://artspacelansdowne.com/workshops/#<?php the_ID(); ?>"><?php the_title(); ?></a></b><?php the_meta(); ?>
    
    <?php endwhile; ?>
    
    else
    
    echo "There are no Workshops at this time.";
    
    }
    Thread Starter hoffcomm

    (@hoffcomm)

    I get this error…

    Parse error: syntax error, unexpected T_STRING, expecting ‘(‘ in /home/content/12/9195112/html/asl/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 32

    Is this the correct way to use this code?

    <?php
    $catquery = new WP_Query( 'cat=2&posts_per_page=100&order=ASC' );
    
    If have_posts {
    
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <b><a href="https://artspacelansdowne.com/workshops/#<?php the_ID(); ?>"><?php the_title(); ?></a></b><?php the_meta(); ?>
    
    <?php endwhile; ?>
    
    else
    
    echo "There are no Workshops at this time.";
    
    }
    <?php endwhile; ?>

    Sorry you need to make the if statement syntax right ..I just was giving you an idea how to do what you want to do.

    https://codex.www.ads-software.com/Function_Reference/have_posts

    <?php
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		// Your loop code
    	endwhile;
    else :
    	echo wpautop( 'Sorry, no posts were found' );
    endif;
    ?>

    based on the reply by @radices, but using your exact initial code:

    <?php
    $catquery = new WP_Query( 'cat=2&posts_per_page=100&order=ASC' );
    
    if($catquery->have_posts()) :
    
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <b><a href="https://artspacelansdowne.com/workshops/#<?php the_ID(); ?>"><?php the_title(); ?></a></b><?php the_meta(); ?>
    
    <?php endwhile; 
    
    else:
      echo wpautop( 'There are no Workshops at this time.' );
    endif;
    ?>
    Thread Starter hoffcomm

    (@hoffcomm)

    THANK YOU alchymyth!!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘If Category has no posts display text’ is closed to new replies.