• Hi, I am using an if elseif statement to check what page it is, and it requires that there are posts on the page so when a search is returned with a result of 0, my code stops working. Any ideas how to code this better?

    this code is in my sidebar and is showing the recent articles, you can see an example of a search working here(the sidebar is setup as a sub-footer):
    http://ivry.sweetyams.ca/?s=new

    and a search not working here:
    http://ivry.sweetyams.ca/?s=asjdfkl%3B

    Code I am using:
    (I have tried putting stuff into the else{ code and it doesn’t work either because there are no posts on the ‘nothing found’ search page

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Try adding

    <?php else : ?>
    <p>No results were found</p>

    before your <?php endif; ?>

    Thread Starter sweetyams

    (@sweetyams)

    Hi Scoe, thanks for the reply, the problem is not with the main content, (the search)

    I am trying to get all recent articles for my sidebar and when there is no result in the search, it also stops my sidebar from working.

    The code I added is not in my search page but in my SIDEBAR, it separates the comments into 2 categories, (6 and 14) I am essentially splitting my site using categories for navigation, anything as a child below 6 will be styled one way and 14 the other, you can see the difference here:

    http://ivry.sweetyams.ca/escalade/
    http://ivry.sweetyams.ca/montagne/

    This is getting every post below escalade, OR every post below the montagne category

    My search pulls results from ALL categories, but if there is no search result, for some reason my sidebar code, the code I included, doesn’t work.

    If this doesn’t make sense sorry!, and thanks.

    OK I think I understand what you are getting at.

    I’m not sure on a better way of coding it, maybe creating separate functions with their own custom loop (using WP_Query instead of query_posts). This would prevent a loop inside a loop.

    In regards to your code stopping, I don’t think the function ‘is_category_or_sub()’ is a WordPress function (unless you have created one).

    See if this will help: http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category

    Thread Starter sweetyams

    (@sweetyams)

    Hi Scoe, thanks for the help!, I used WP_query and it still didn’t work, but then after rearranging the code to be a little more efficient, I got it to work!

    code is below:

    <?php
    $my_posts = new WP_Query(); ?>
    <?php /* IF SEARCH PAGE */  if (is_search() ) { ?>
    <?php $my_posts->query('showposts=5'); ?>
    <?php /* IF ESCALADE PAGE */ }elseif (is_category_or_sub(6)) { ?>
    <?php $my_posts->query('showposts=5&cat=6');?>
    <?php /* IF MONTAGNE PAGE */ } elseif (is_category_or_sub(14)) { ?>
    <?php $my_posts->query('showposts=5&cat=14'); ?>
    <?php } else { ?>
    <?php $my_posts->query('showposts=5'); ?>
    <?php }?>
    
    <?php while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts ?>
    	<a href="<?php echo esc_url( get_permalink( $post->the_permalink ) ) ?>" title="Permanent Link to <?php echo substr($post->post_title,0,200);?>">
    	<p><?php echo substr($post->post_title,0,200); ?></p>
    	</a>
    <?php endwhile; ?>

    p.s. the is_category_or_sub() is a function I found here!

    thanks for the help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting Posts when Search result is Nothing’ is closed to new replies.