Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Function.php doesn't work where search.php when no results found

I'm developing a custom Wordpress site, in Blog section i used the widget Wordpress sidebar, and when a use search widget to find a word that exist in the site, it works fine, but when i search for a missing word in the site, the side breaks, all the stylesheet and JS dont get included. In my point of view, when the search has no results, Wordpress dont "run" the function.php.

CODE :

<div class="row" align="center" >
    <div class="container"> <div style="height: 60px;width: 100%"></div>    
        <h1 style="color: #087ba7;">Resulta da busca: <?php echo $s; ?></h1> 
    </div>
</div>

             <div class="row" align="center" >
                        <div class="container">
                            <div class="span9">
                                <?php if(have_posts()): 
                                        while(have_posts()): the_post(); ?>
                                            <div class="span9" style="text-align: left;margin-top: 50px;">
                                                <h1 style="font-size: 18.5px;"><?php the_title(); ?></h1> 
                                                <hr />
                                                <div class="span2 post_thumb div-img-circle">
                                                    <a href="<?php the_permalink(); ?>">
                                                        <?php echo the_post_thumbnail(); ?>
                                                    </a>
                                                </div>
                                                <div class="span6" style="text-align: justify;">
                                                    <a href="<?php the_permalink(); ?>" style="text-decoration: none;">

                                                        <?php the_excerpt(); ?>
                                                    </a>
                                                    <div style="width: 100%;text-align: right">
                                                        <a href="<?php the_permalink(); ?>">Leia Mais</a>  
                                                    </div>
                                                </div>
                                            </div>
                                <?php   endwhile; ?>
                                <?php else : ?>
                                    <?php wp_reset_query();?>
                                <div class="span9" style="text-align: left;margin-top: 50px;">
                                    <h1 style="font-size: 18.5px;"><?php echo wpautop( 'Sorry, no posts were found' ); ?></h1> 
                                </div>
                                <?php endif; ?>
                            </div>
                            <div class="span2" style="text-align: left;">
                                    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar') ) : ?><?php endif; ?>
                            </div>
                        </div>
                    </div>


                    <div class="row" align="center" >
                        <div class="container"> 
                            <?php custom_pagination(); ?>
                        </div>
                    </div>

6 Answers

I could solve the problem. I make a copy of twelve theme folder and copy the content of my custom files inside the same files in the new folder and it just work.

Thanks anyway

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Not sure it will solve it, but try commenting out the reset loop line in the else statement for when there are no search results. If you remove the else statement completely, does the page work?

I remove it but it still display the same.

I make i few more tests and wordpress dont include anything inside <head> Tags, i code some Css inside the Dev and even this is imported in the page

I make another change, i remove the IF and get the same error, when i search a word that dont exist, wordpress doesnt load anything inside <header>

<?php// if ( have_posts() ) : ?>
                               <h1> Resultado da busca por: <?php echo $s; ?> </h1>

                <?php while ( have_posts() ) : the_post(); ?>
                                                <div class="span9" style="text-align: left;margin-top: 50px;">
                                                    <h1 style="font-size: 18.5px;"><?php the_title(); ?></h1> 
                                                    <hr />
                                                    <div class="span2 post_thumb div-img-circle">
                                                        <a href="<?php the_permalink(); ?>">
                                                            <?php echo the_post_thumbnail(); ?>
                                                        </a>
                                                    </div>
                                                    <div class="span6" style="text-align: justify;">
                                                        <a href="<?php the_permalink(); ?>" style="text-decoration: none;">

                                                            <?php the_excerpt(); ?>
                                                        </a>
                                                        <div style="width: 100%;text-align: right">
                                                            <a href="<?php the_permalink(); ?>">Leia Mais</a>  
                                                        </div>
                                                    </div>
                                                </div> 
                                <?php endwhile;
                //else :
                                ?>
<!--                                    <div class="span9" style="text-align: left;margin-top: 50px;">
                                        <h1 style="font-size: 18.5px;">Não encontramos nenhuma informação pela busca de: <?php echo $s; ?></h1>
                                    </div>-->
                                <?php

I Also remove the While leaving only

<?php get_header(); ?>

        <div class="row" align="center" >
            <!-- Wrap the .navbar in .container to center it within the absolutely positioned parent. -->
            <div class="container"> <div style="height: 60px;width: 100%"></div>
                <div style="width: 90%;text-align: left;margin-bottom: 100px;margin-top: 20px;" class="contentinfo">
            <?php// if ( have_posts() ) : ?>
                               <h1> Resultado da busca por: <?php echo $s; ?> </h1>


        </div><!-- #content -->
    </div><!-- #primary -->
        </div>        

<?php
get_footer();

And still got the problem