2

what i did,

i create a search.php

<?php get_header(); ?>
<div class="wapper">
  <div class="contentarea clearfix">
    <div class="content">
      <h1 class="search-title"> <?php echo $wp_query->found_posts; ?>
        <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>" </h1>
      <ul>
        <?php query_posts('showposts=3'); if (have_posts()) : ?>
        <li>
          <h3><a href="<?php echo get_permalink(); ?>">
            <?php the_title();  ?>
            </a></h3>
          <?php  the_post_thumbnail('medium') ?>
          <?php echo substr(get_the_excerpt(), 0,200); ?>
          <div class="h-readmore"> <a href="<?php the_permalink(); ?>">Read More</a></div>
        </li>
        <?php endif ?>
      </ul>
    </div>
  </div>
</div>
<?php get_footer(); ?>

then create a searchform.php

<form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get">
  <input type="search" id="searchbox" name="s" placeholder="Enter keywords" required>
    <input type="image" id="searchsubmit" alt="Search" class="searchicon" src="<?php bloginfo( 'template_url' ); ?>/images/icon-search.gif" />
</form>

but my result page not showing content description and also show only one result. but on title H1 it showing 2/3/5/6/7 etc number Search Result Found. i want show at least 10 result and then page and also show 200/300 characters content text for search result. guys can help me. i am not expert wp yet

7

I have some recommendations for your question:

First: stop using query_posts(). See the codex about this function to see why you shouldn't use it in themes or plugins. Anyway, if you are in a some weird situation where you have not option and you need to use query_posts(), you should run wp_reset_query() after the loop. You must know that you are using the global $wp_query, that contains the original query made by WordPress, and then query_post that alter the global $wp_query variable, you end up with unexpected results. Additionally, you are using a deprecated parameter showposts, replaced by posts_per_page.

Second: you can use a custom search template (search.php) to customize the look and feel. Just cerate a search.php file in your theme folder and customize it as you want. Don't make custom queries here; if you do that, you are making a new query for posts and wasting the query already done by WordPress. A waste of resources with negative performance impact.

Third: to change the default query parameters used by WordPress, like number of posts per page, etc, you can use pre_get_posts action.

So, create a your search.php template as you wish and use pre_get_posts action to say to WordPress what parameters you want to use in the search query:

The search.php template could be something like this:

<?php
get_header();
global $wp_query;
?>
<div class="wapper">
  <div class="contentarea clearfix">
    <div class="content">
      <h1 class="search-title"> <?php echo $wp_query->found_posts; ?>
        <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>" </h1>

        <?php if ( have_posts() ) { ?>

            <ul>

            <?php while ( have_posts() ) { the_post(); ?>

               <li>
                 <h3><a href="<?php echo get_permalink(); ?>">
                   <?php the_title();  ?>
                 </a></h3>
                 <?php  the_post_thumbnail('medium') ?>
                 <?php echo substr(get_the_excerpt(), 0,200); ?>
                 <div class="h-readmore"> <a href="<?php the_permalink(); ?>">Read More</a></div>
               </li>

            <?php } ?>

            </ul>

           <?php echo paginate_links(); ?>

        <?php } ?>

    </div>
  </div>
</div>
<?php get_footer(); ?>

And the pre_get_posts action something like this:

add_action( 'pre_get_posts', function( $query ) {

    // Check that it is the query we want to change: front-end search query
    if( $query->is_main_query() && ! is_admin() && $query->is_search() ) {

        // Change the query parameters
        $query->set( 'posts_per_page', 3 );

    }

} );
  • 1
    Not really necessary to use global $wp_query when in template context, you can just use $wp_query object where necessary, but I guess better be safe than sorry :-) – Pieter Goosen May 11 '15 at 11:59
  • 1
    I was not sure about that, so I included the global reference. Thanks for the note. – cybmeta May 11 '15 at 12:33
  • @cybmeta thanks it work nice. thank you again. if you never mind then you can tell me how to add paging ? – pagol001 May 11 '15 at 20:41
  • @PieterGoosen so do you think i better replace global $wp_query; to $wp_query; – pagol001 May 11 '15 at 20:44
  • I didn't include pagination because you had not pagination in your code, I just copy your code without the custom query part. You could use any of the functions provided by WordPress for multiple posts pagination. – cybmeta May 11 '15 at 21:36
0

You have to run the loop , modify your search.php something similar to the code below

  <?php get_header();?>

    <div class="wapper">
      <div class="contentarea clearfix">
        <div class="content">
            <ul>
                <?php if ( have_posts() ) : ?>

            <header class="page-header">
                <p><?php printf( __( 'Search Results for: %s', 'twentyfourteen' ), get_search_query() ); ?></p>
            </header><!-- .page-header -->

                        <?php
                        // Start the Loop.
                        while ( have_posts() ) : the_post();
                        ?>
                        <li><h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3></li>
                        <?php the_post_thumbnail('medium') ?>
                        <?php echo substr(get_the_excerpt(), 0, 200); ?>
                            <div class="h-readmore"> 
                                <a href="<?php the_permalink(); ?>">Read More</a>
                            </div>
                        <?php
                        endwhile;
                else :
                // If no content, include the "No posts found" template.
                get_template_part( 'content', 'none' );
                endif;
                ?>       
            </ul>                                 

        </div>
      </div>
    </div>
<?php get_footer(); ?>
  • In the above code change 'twentyfourteen' to your theme text domain – shuvroMithun May 11 '15 at 9:53
  • thanks. if giving me error Parse error: syntax error, unexpected end of file in .../search.php on line 34 – pagol001 May 11 '15 at 20:45

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.