0

I have a problem where the page title is not picked up in search results.

I've narrowed the problem down to the following line of search.php in my custom theme.

define('BodyID', 'search'); the_post();

When I include the above line, page titles are not picked up in search results, and when I take those lines out and run a search query I get the expected search results.

Any idea why this could be interrupting the search?

The full code for search.php is below.

<?php
define('BodyID', 'search'); the_post();
include(TEMPLATEPATH."/inc/header.php"); ?>
    <div id="main_content">
        <div class="sleeve clearfix">
            <?php if ( have_posts() ): ?>
            <h1>Search Results for '<?php echo get_search_query(); ?>'</h1>
            <ol class="search-results">
            <?php while ( have_posts() ) : the_post(); ?>
                <li>
                    <article>
                        <h2><a href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h2><a class="more-link" href="<?php esc_url( the_permalink() ); ?>" title="Permalink to <?php the_title(); ?>" rel="bookmark">Read more&hellip;</a>
                </article>
            </li>
        <?php endwhile; ?>
        </ol>
        <?php custom_page_navi(); ?>
        <?php else: ?>
        <h1>No results found for '<?php echo get_search_query(); ?>'</h1>
        <p>Please try another search, or return to the <a href="/">home page</a>.</p>
        <?php endif; ?>
    </div>
</div><?php
include(TEMPLATEPATH."/inc/footer.php"); ?>

And here's the relevant part of header.php which sets the body ID. This is the top few lines.

<?php
# Create Body ID
(BodyID == "" || BodyID == "BodyID") ? $body_id = "page" : $body_id = BodyID; ?>
<!DOCTYPE html>
...

Thanks.

  • No idea without seeing contents of that header file?.. – Rarst Jul 29 '14 at 14:29
  • Ah, of course, I hadn't thought of that. Will add above. – jasonbradberry Jul 29 '14 at 14:34
  • It doesn't seem to be the header.php file itself, as I've tried searching with the header.php file reduced to a blank file, and still getting incorrect search results. When I remove the define('BodyID', 'search'); the_post(); line I get correct search results, so it's something to do with that line. – jasonbradberry Jul 29 '14 at 14:56
0

You should call the_post only inside the loop, or call rewind_posts() before actually starting the loop.

sidenote: you are doing it totally wrong. don't reinvent the wheel, look in the codex how to write a theme or use one of the bundled default themes as a base to your theme. No sane person starts to code a theme from zero.

| improve this answer | |
  • Thanks, this fixed it. When you say don't reinvent the wheel, are you just referring to my use of the_post or more about my general approach? – jasonbradberry Jul 29 '14 at 16:17
  • 1
    general approach of including files. header related code for example should be in the header.php file of the theme and there is a dedicated function get_header IIRC to load it which triggers some actions that can be used to extend functionality, but more important it is a structure that all wordpress developers are familiar with and the guy that will have to maintain your code after you will have an easier time to understand what is going on if you follow the conventions. – Mark Kaplun Jul 29 '14 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.