0

I am wondering if this could be done if i want something like this. Look at my code please.

Here the code goes:

<?php _e('Search result for','NCC'); ?> 
<?php /* Search Count */ 
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1); 
    $count = $allsearch->post_count; 
    _e(''); 
    _e('"<span>'); 
    echo $key; 
    _e('</span>"'); 
    _e('<span class="resultsFound">( We found '); echo $count . ' '; _e('companies )</span>'); 
    wp_reset_query(); 
?>

The outcome of this code will be:

( We found 1 companies )

Is it possible to make it work like the result saying "company" when there is 0 or 1 result found and saying "companies" when there are more 2 or more results found when we using the wp search box?

Example:

(1 company found) or (2 companies found)

Any suggestion? Is it possible with wordpress?

| improve this question | | | | |
  • I'm sure someone will look at your code if it is properly formatted. Your complete code section is in one looooonnnnggg line, making it unreadable. Please file an edit and correct your code formatting – Pieter Goosen Mar 5 '15 at 12:30
  • Hi Pieter Goosen, thanks for advises! But, how should i use this code? I am a newbie in php coding. Need more detailed instruction. Appreciate it! :) – Jornes Mar 5 '15 at 14:40
  • I have formatted your code to make it readable. Remember in future, if your posts aren't properly formatted or your code is unreadable, others tend to just skip and ignore your question :-) – Pieter Goosen Mar 5 '15 at 16:05
2

$found_posts holds the amount of posts found by a certain query. You can use this logic to display your links

Example:

if ( $allsearch->found_posts <= 1 ) {
    //Dispaly company
} else {
    // Disply companies
}

Just a note on grammar, it should be 0 companies 1 company and x amount of companies after that

EDIT

From the edit I have done to your question, I have the following: To be honest, your code is a bit of a mess, but nothing that cannot be fixed though :-). Lets take a quick run down

  • showposts is depreciated in favor of posts_per_page

  • wp_specialchars has been depreciated since v2.8.0. You should be using esc_html. But to be honest, I don't know if it is appropriate here.

  • You should not localize HTML tags, just literal text should be localized. Exclude HTML tags from strings to be localized and make use of placeholders

  • wp_reset_query() should be used with query_posts which you should never ever use. You should be using wp_reset_postdata() with WP_Query. In this case it is not necessary as you are not setting up postdata or changing the global $post variable

  • I'm not very sure if you really need to localize the value of $key

As already stated in the other answer, you can make use of _n() to localize strings with single and plural meaning. As I have already stated, the correct grammar and use is

  • 0 companies 1 company and x amount of companies

Your issue is in this line

_e('<span class="resultsFound">( We found '); echo $count . ' '; _e('companies )</span>'); 

We can rewrite it to something like this

$text  = '<span class="resultsFound">';
$text .= sprintf( _n( 'We found %d company', 'We found %d companies', $count ), $count );
$text .= '</span>'; 
echo $text;

If you really need to display 0 company, you can use

$text  = '<span class="resultsFound">';
if ( $allsearch->found_posts <= 1 ) {
    $text .= sprintf(__( 'We found %d company' ), $count );
} else {
    $text .= sprintf(__( 'We found %d companies' ), $count );
}       
$text .= '</span>'; 
echo $text;

or

$text  = '<span class="resultsFound">';
if ( $allsearch->post_count <= 1 ) {
    $text .= sprintf(__( 'We found %d company' ), $count );
} else {
    $text .= sprintf(__( 'We found %d companies' ), $count );
}       
$text .= '</span>'; 
echo $text;
| improve this answer | | | | |
  • Please see my update. This should get you going – Pieter Goosen Mar 5 '15 at 16:43
  • Hi Pieter Goosen, thanks for your explanation and a clear instruction! I follow the steps and now it works. I will make every question readable in future. Again, thanks for your help! I appreciate it. :) – Jornes Mar 5 '15 at 17:26
  • No problem, my pleasure. Glad you got this solved. Enjoy :-) – Pieter Goosen Mar 5 '15 at 17:32
0

You may use _n function instead of _e.

Read this: _n

| improve this answer | | | | |

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.