Forums

[resolved] WP Search Results (25 posts)

  1. revlimiter
    Member
    Posted 1 year ago #

    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,

  2. nublooo
    Member
    Posted 1 year ago #

    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

  3. revlimiter
    Member
    Posted 1 year ago #

    Hey nublooo,
    Thanks for the very helpful response! I have adjusted the code on my Search Results page, http://www.kimstewart.ca/?s=test

    Here 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,

  4. nublooo
    Member
    Posted 1 year ago #

    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: http://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 :)

  5. whooami
    Member
    Posted 1 year ago #

    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 :)

  6. nublooo
    Member
    Posted 1 year ago #

    whooami,
    look above :)

  7. whooami
    Member
    Posted 1 year ago #

    I saw, right after I posted :)

  8. revlimiter
    Member
    Posted 1 year ago #

    Hey nublooo,
    Well it is definitely working for the pagination on previous pages, check out http://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,

  9. revlimiter
    Member
    Posted 1 year ago #


  10. nublooo
    Member
    Posted 1 year ago #

    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
    Nubloo

  11. revlimiter
    Member
    Posted 1 year ago #

    Hey 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,

  12. whooami
    Member
    Posted 1 year ago #

    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:

    http://wordpress.org/extend/plugins/results-count/

    it was just updated for 2.6.x 3 weeks ago i guess.

  13. revlimiter
    Member
    Posted 1 year ago #

    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!!

  14. taghaboy
    Member
    Posted 1 year ago #

    thanks you all for this useful information.

  15. nublooo
    Member
    Posted 1 year ago #

    Glad to know it works revlimiter!

    To display thumbnails, you can make use of WP's custom fields. I'd start here: http://codex.wordpress.org/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
    Nubloo

  16. taghaboy
    Member
    Posted 1 year ago #

    Hi 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

  17. nublooo
    Member
    Posted 1 year ago #

    Hi taghaboy,

    Using a condition like you do is a good idea, I always check if a custom field exists also. In order to achieve your standard image, you can just use the same structure as in line 3 of your code.

    Line 5 would be like this:

    else { ?>
    *your no-img html markup*
    <?php } ?>

    BUT, 2 things:
    1.
    Consider writing the get_post_custom_values() in a variable right after the wordpress loop starts (before the POST div), and echoing that variable. In PHP, it is always faster to echo something that's pre-stored in a variable than it is to call it inline. I would also say it is a sign of good and clean code.

    2.
    What I do and I would recommend you do also is having a default background image for that defined in CSS. Then you can just leave the ELSE part out and simply close your PHP code after line 4 of your code above. If no image is there, the call doesn't display anything and the background image is viewable. Just make sure to define width and height of the img in CSS so that it keeps its size.

    That's cleaner, results in less code, and you can always change everything from CSS instead of having to go through every instance of your PHP call when you need to change the path or something else.

    Hope that helped.

  18. revlimiter
    Member
    Posted 1 year ago #

    Hey taghaboy,
    Can you share how you made the thumbnails work on your search results pages? I am interested in checking out your Wordpress blog. :)

    Thanks!

  19. Dgold
    Member
    Posted 1 year ago #

    @ whooami

    I just tested out the plugin results-count 0.3

    Before this I was using (and I saved) results-count-updated 0.2. If I recall correctly this was an alternate version of the original plugin, which was modified by you (whooami) sometime earlier, some months ago.

    results-count-updated 0.2 WORKS on my search pages. For example, "Showing 43 results for the search terms: light."

    results-count 0.3 DOES NOT WORK on my search pages. I get "Showing 0 results for the search terms: light."

    I reverted back to the results-count-updated plugin since it works for me on Search pages. But I would like to upgrade to the new plugin and implement this counter on other pages like categories.

    Any idea what's wrong with 0.3, for me? Do you recall what the fix was last time?

    I don't want to post my link on here but I will email it to you if you want to see, whooami. Thank you very much for your help.

  20. whooami
    Member
    Posted 1 year ago #

    Any idea what's wrong with 0.3, for me? Do you recall what the fix was last time?

    I don't want to post my link on here but I will email it to you if you want to see, whooami. Thank you very much for your help.

    nope, and yeah I remember all of that.

    maybe drop me an email, I want to ask a couple q's that you might not want to answer here.

    I cant make any big promises though, I'm working all this weekend, and having some work done on my new "place to live", and having a new roomate move in, and am previously committed to 2 other wordpress gigs right now.

    not enough hours in the day, right now, for me.

  21. nublooo
    Member
    Posted 1 year ago #

    revlimiter,

    If you follow the link I pasted to the custom fields page on WP above, you can see how to implement thumbnails. It's not that hard at all.

    An overview for you:
    - Upload an image that you want to serve as a thumbnail to a post, but don't embed it in the post
    - Get the image ID from the Media Library in your Wordpress Admin
    - Back in the post, create a custom field named "thumbnail" and as a value, enter the image ID
    - In your search PHP file, put the code taghaboy posted above in the wordpress loop where you want the thumbnail to appear. Replace "petit" with "thumbnail"
    - That's it

    Just check out the link I pasted.

  22. taghaboy
    Member
    Posted 1 year ago #

    Hi nublooo,
    thanks for advice and help too, the idea behind the bg-img sound so good, i'll use it in many project.
    some thing i dont understand it's the code steel not work and it dont echo the url of img!!!

    <?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 { ?>
    <img src="<?php bloginfo('stylesheet_directory'); ?>/images/no-img.gif" />
    <?php } ?>

    what the : if($value !== '') { ?> do exactly.
    * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
    The tips of "number each result" work perfectly, but whil i click for next result the number count to number one again( 1-2-3-4=page one ::: 1-2-3-4=page two... and other page result)
    her the code:

    <?php get_header($postnum = 1); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
            <div class="count">
    <?php echo $postnum; ?>
    	</div>
    
    	<p>lorem ipsum dolar set</p>
    
    <?php $postnum++; ?>
    <?php endwhile; ?>
    
    	<div class="page-nav">
    		<span class="previous-entries"><?php next_posts_link('Next') ?></span>
    		<span class="next-entries"><?php previous_posts_link('Previous') ?></span>
    	</div>
    
    <?php else : ?>
    <?php endif; ?>
    <?php get_footer(); ?>

    ______________________________________________________________
    revlimiter : exactly as nublooo sad, her the code to only show the image url you set in the custom field named "thumbnail", her i add a div "thumb" it help me to align img to the left of every search result with the style:
    .thumb {
    float:left;
    margin-right:15px;
    }

    <div class="thumb">
    <img src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>">
    </div>
  23. revlimiter
    Member
    Posted 1 year ago #

    Thanks nublooo and taghaboy! Your help has been fantastic, and I managed to get the search result thumbnails working.
    Check it out:
    http://www.kimstewart.ca/?s=skyline&submit.x=0&submit.y=0&submit=Search

    I think I'm gonna put them on my "Entries Tagged As" results pages also.

    Quick question about a plugin I'm using, Kimili Flash Embed. If you goto this page you will see it in there with the text:
    http://www.kimstewart.ca/?s=google&submit.x=0&submit.y=0&submit=Search

    Does anybody have any experience removing this from the search results page?

    Cheers,

  24. taghaboy
    Member
    Posted 1 year ago #

    Happy that you we help you,
    for the video who appear in ur search result, i really don't have any idea, sorry.
    _________
    tell me, did you have like this problem:
    The tips of "number each result" work perfectly, but whil i click for next result the number count to number one again( 1-2-3-4=page one ::: 1-2-3-4=page two... and other page result)

  25. taghaboy
    Member
    Posted 1 year ago #

    did you revlimiter understand my question?
    thanks

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.