• Resolved ThaiClimate

    (@thaiclimate)


    I’m using (trying to use) the search.php to choose one of two templates (English or Thai) for returning results depending on where the search function was executed — from the English side where everything is displayed from one parent category or from the Thai side with a different parent category. My search.php can easily parse the parent category and display using the appropriate language template when it has a result, but when “no result” , I need another way for the search.php to know which language template to choose.

    Question 1: Is it possible to set/use a variable in the searchform.php that can still be accessed from the search.php after the search has been performed?

    Also, when “no result” occurs wp displays “Sorry, no posts matched your criteria”. I can’t find where I can edit or set any kind of condition for this default message.

    Question 2: Should I be inserting a conditional statement in my search.php file that selects my own “no result” message (as opposed to this default “no result” message) to display when finding no results? If so, could someone provide me with the relevant code for testing for “no results found” from the search function?

    I’ve read in the codex and the forums about setting up search pages, etc., but these two issues do not seem to be addressed.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter ThaiClimate

    (@thaiclimate)

    Still wondering how I might be able to set a simple variable, say English or Thai, to help when using template selection between pages/posts/searches, etc.

    As to question 2, am now using: <?php if (have_posts()) {?> … to generate my own bilingual text and a back button as a sort of an error page. Not pretty, but will hold until I can figure out an answer to Question 1.

    Also for those who may be looking for back-button code, which took me awhile to locate, I used Back

    Thread Starter ThaiClimate

    (@thaiclimate)

    I’ve resolved the problem of setting and passing variables within the search process. May not be the best way, but it seems to work for me.

    I’ve used a HIDDEN form variable “lang” to append as an extra variable to the “s” variable passed within the url by the searchform.php to the seach.php.

    Actually, I’ve set-up two searchforms, searchformthai.php and searchformeng.php depending on what language template the user is starting the search from. In each of the sarchforms, after the line
    <input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />

    I’ve added this for searchformthai.php:
    <input type=”hidden” name=”lang” value=”thai”/>

    I’ve added this for searchformeng.php:
    <input type=”hidden” name=”lang” value=”eng”/>

    This causes the url passed from the searchform.php to search.php to now contain two variables “s” and “lang” and looks like:
    https://mysite/index.php?s=searchstring&lang=thai
    or
    https://myrsite/index.php?s=searchstring&lang=eng

    Then in the search.php I’ve retrieved the “lang” variable and used it as a condition in an IF statement to determine which templates, Thai or English, should be used to display along with the results.

    At the top of the search.php file I’ve added:

    <?php $lang = $_GET[“lang”];?>

    <?php if ($lang == “eng”) { ?>
    …use English templates…

    <?php }else{ ?>
    …use Thai templates

    <?php } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Passing variable from searchform.php to search.php’ is closed to new replies.