WP Search Results
-
I have a couple of questions regarding WordPress Search Results:
Have a look at this page on my blog.
In this example, I searched for “website”.
Is there any way to:
-Number each result?
-Have the keyword appear at the top (Search Results for “Website”)
-Change the URL structure “?s=website&submit.x=0&submit.y=0&submit=Search”?Any help is greatly appreciated!
Thanks!
Cheers,
-
Hi,
To number each result:
In your theme’s search.php, add a variable that increases with each loop, and echo it for each loop:
1) Directly under<?php get_header(); ?>
, add$postnum = 1;
2) Inside the loop, where you want the number to appear, add<?php echo $postnum; ?>
– e.g. right before the title tag is called.
3) Right before<?php endwhile; ?>
, add `<?php $postnum++; ?> – this will increase the variable by one.If you want to carry the last number on to the next page on paginated results, that would require a bit more coding, but this should get you started.
To have the keyword appear on the page, paste this where you want it to be (outside the loop):
Search Results for <?php the_search_query(); ?>
About your URL structure, I don’t know how the submit.x, submit.y and submit parameters are getting in there. It would help if you posted the contents of your searchform.php file in here.
Cheers
Hey nublooo,
Thanks for the very helpful response! I have adjusted the code on my Search Results page, https://www.kimstewart.ca/?s=testHere is the code that is running in searchform.php (in regards to the URL structure).
<form method="get" id="search_form" action="<?php bloginfo('home'); ?>/"> <input type="text" class="text_input" value="To search, type and hit enter" name="s" id="s" onfocus="if (this.value == 'To search, type and hit enter') {this.value = '';}" onblur="if (this.value == '') {this.value = 'To search, type and hit enter';}" /> <input type="hidden" id="searchsubmit" value="Search" /> </form>
Also, do you know how I can get the full $postnum; value in front of “Search Results”? So if there is more than 1 page of search results I guess I will need it to reflect the pagination.
Thanks again!
Cheers,Hi revlimiter,
Do you need that JavaScript in your searchform? All of that onfocus and onblur. I’m not entirely sure if that’s the cause but try removing that and see if it still has the same URL structure.
The input-text tag without JavaScript:
<input type="text" class="text_input" value="To search, type and hit enter" name="s" id="s" />
What the JS does is remove the text “To search, type and hit enter” from the search box when user clicks on it, and bring it back when user clicks out, and it’s an inline IF condition.
I’d first try to find out if that is the cause at all. If it is, try outsourcing the JavaScript code to a .js file or at least outside of the form, and only calling it from the form (take a look at this example: https://www.fireandknowledge.org/archives/2006/11/03/clearing-fields-with-javascript/ )
To carry the $postnum count on to following pages, my first thought would be using the global variable $paged which holds the current page number, something like this:
<?php //Initialize $pagenum $pagenum = $paged; //Set 1st page to 1 instead of 0 if($pagenum == ''){$pagenum = 1;} //Start checking which page is up if ($pagenum == 1) { //On 1st page start counting posts from 1 $postnum = 1; } else { //On following pages start counting from 11, 21, 31, etc. $postnum = ($pagenum * 10) - 9; } ?>
Not sure if this will work, I didn’t have time to test it. Give this a try and tell me if it works ??
If you want to carry the last number on to the next page on paginated results, that would require a bit more coding, but this should get you started.
thats not hard to do — Ive actually done this both ways, numbering ascending and descending, through all pages. I dont have the code handy or I would share it, but it wasnt that hard to do, and it certainly didnt need a ‘bit’ more coding. It was like 3 lines ??
whooami,
look above ??I saw, right after I posted ??
Hey nublooo,
Well it is definitely working for the pagination on previous pages, check out https://www.kimstewart.ca/?s=the&submit.x=0&submit.y=0&submit=Search and click the bottom link “← Previous Entries”. ??Thanks!!!
Now, how do I get the full pagenum value in front of “Search Results for” at the top?
Cheers,
If you mean the number of pages the search results are spread across, then try using the global WP call $wp_query->max_num_pages – I’d write that value into your own variable first, like so:
$maxpages = $wp_query->max_num_pages;
And then you can play with it and echo it in with the Search Results:
<?php echo $maxpages; ?>
Is that what you mean?
Cheers
NublooHey nublooo,
Thanks for the reply.That’s good code to know but in fact I was referring to the number of search results.
So for example, if I search for “About” I will get “23 Search Results”Cheers,
theres a simpler way to do this, but I cant find it, and Im not sharing my own convoluted (not so simple) code, so here’s a plugin:
https://www.ads-software.com/extend/plugins/results-count/
it was just updated for 2.6.x 3 weeks ago i guess.
Thanks whooami!
That’s a great plugin, it does the trick ?? Thanks for sharing!Do you know if there are any 2.6 compatible plugins for thumbnails? What I mean by this is, when a user does a search or browse by tag, I would like a thumbnail to appear beside the text writeup. This will just give a representation of the post and I think it will really help with the UI experience.
Thanks!!
thanks you all for this useful information.
Glad to know it works revlimiter!
To display thumbnails, you can make use of WP’s custom fields. I’d start here: https://codex.www.ads-software.com/Using_Custom_Fields
It’s explained pretty well there.
That’s a whole other topic though. I’d start a new thread for anything related to that.
Cheers
NublooHi nublooo,
your tips are really good,
i’v started with showing the thumb in search result and it work perfectly,
her a if else who help me to show my img, if not it will show another image i chose “no-img.jpg. But i have some probleme who dont let me to see my no-img.jpg :<?php // if there's a img in my custom field if($value !== '') { ?> <img src="<?php $values = get_post_custom_values("petit"); echo $values[0]; ?>"> <?php } // end if statement // if there's no img else { echo 'link of img'; } ?>
is it possible to put this line:
<img src='<?php bloginfo('stylesheet_directory'); ?>/images/no-img.jpg' />
in :
else { echo 'link of img'; } ?>
thanks
- The topic ‘WP Search Results’ is closed to new replies.