Tuesday, June 30, 2015

Adding Search

WordPress includes a search feature that is easy to implement.  On front-page.php add the following:

front-page.php
<?php get_search_form(); ?>

With this in place, the search results page is defaulting to index.php which does not work. We will start with copying page.php to search.php. We then will make some refinements.

search.php
<html>
  <head>
    <title>Barebones</title>
    <?php wp_head() ?>
  </head>
  <body>
    <?php wp_nav_menu(array('theme_location' => 'header-menu')); ?>
    <?php if (function_exists('yoast_breadcrumb')) {
      yoast_breadcrumb();
     }?>
    <?php get_search_form(); ?>
    <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
        <p><b><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></b></p>
      <?php endwhile; ?>
    <?php else : ?>
      <p>No matches.</p>
    <?php endif; ?>
    <?php wp_footer() ?>
  </body>
</html>

No comments:

Post a Comment