Use _n to write a phrase in single or plural form

If you would want to write a phrase in single or plural form depending on a number, there is a very handy function for it written in WordPress called _n. The function also takes a $domain argument which can be handy when localizing plugins or themes.

In your search template you might want to print “Found X results matching your query”. When X is above 1 you want to write “results” but if there only was a single result found you would want to write “result”.

This can be accomplished with the following code:

<?php printf( __( 'Found %d %s matching your query: %s' ), $wp_query->found_posts, _n( 'result', 'results', $wp_query->found_posts), get_search_query() ); ?>

More information about _n can be found in the codex!

About

Web Developer @ Oakwood Creative - www.oakwood.se

Tagged with: , , , , ,
Posted in Best Practices, Intermediate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About

WordPress Quick Tips is a blog supplying great tips about WordPress.

We hope to create a great knowledge resource for WordPress developers as well as serving a reminder for all the forgetful ones.

The blog is created and run by Vincent of Oakwood Creative

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 121 other subscribers
%d bloggers like this: