• Resolved cjorgenson

    (@cjorgenson)


    I am not a strong PHP coder and am having issues adding this functionality to my search results page. I can follow the normal if/else statements that are found online but the theme i am using has added complexity and every time i try to add an if statement to tell it what to display when found_posts<1 it just breaks the whole page. Can someone please give me a hint or show me what to input where?

    Search page code is as follows:

    <?php get_header();
    global $wp_query; ?>
    
    <div style="background: #fff;" id="container">
        <div style="background: #fff;" class="container">
            <div class="row">
                <div class="col-sm-12">
                    <?php if ( have_posts() ) { ?>
    
                        <div class="page-title">
                            <h1><?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"</h1>
                        </div>
    
                        <?php while ( have_posts() ) { the_post();
                            $post_excerpt = substr(get_the_excerpt(), 0,240);
                            $post_excerpt = str_replace('Description', '', $post_excerpt);?>
                                <div class="row">
                                <?php if (has_post_thumbnail()):?>
                                    <div class="col-sm-2">
                                        <a>">
                                            <?php the_post_thumbnail('medium') ?>
                                        </a>
                                    </div>
                                    <div class="col-sm-10">
                                        <h3><a>">
                                          <?php the_title();  ?>
                                        </a></h3>
                                        <?php echo $post_excerpt .'...'; ?>
                                        <div class="read-more">
                                            <a>">Read More</a>
                                        </div>
                                    </div>
                                <?php else: ?>
                                    <div class="col-sm-12">
                                        <h2>
                                            <a>">
                                                <?php the_title();  ?>
                                            </a>
                                        </h2>
                                        <?php echo $post_excerpt; ?>
                                        <div class="read-more">
                                            <a>">Read More</a>
                                        </div>
                                    </div>
                                <?php endif; ?>
    											  
                                </div>
    
                            <hr>
    
                        <?php } ?>
    
                       <?php paginate_links(); ?>
    
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
    
    <?php get_footer(); ?>
    • This topic was modified 5 years, 10 months ago by bcworkz. Reason: code fixed
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There is normally an else element to if have_posts that is missing on this template which would cover what you need. At the closing curly brace below the paginate_links() call, insert the following (replacing the entire closing curly brace line already in place):

                    <?php } else { ?>
                       Sorry, nothing was found.
                     <?php } // end if/else have_posts ?>

    Naturally you can alter the Sorry line as you like.

    BTW, when you post code in these forums, please demarcate with backticks or use the code button. I fixed your OP’s code for you.

    Thread Starter cjorgenson

    (@cjorgenson)

    Awesome, thanks so much! That fixed it. Sorry, new to the forums here. I can generally figure things out on my own so far but this had me stumped. The sloppy coding habits of the previous Dev aren’t helping either.

    Moderator bcworkz

    (@bcworkz)

    Nothing to apologize for ?? Everyone could benefit from outside help once in a while, no matter their experience level. No one can know and see all. Working on other’s code is almost always particularly vexing.

    Welcome to the forums!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding “No results found” and/or other content to the Search results page’ is closed to new replies.