_n

Home » Functions » _n

Function Name: _n

The _n function is used for pluralizing phrases in WordPress. It is commonly used in themes and plugins to handle singular and plural forms of words based on a given number. This function is specifically designed for languages that use plural forms, which can be complex in some cases.

The function requires two parameters: a singular form of the phrase, and a plural form of the phrase. It also requires a number to determine whether the singular or plural form should be used.

For example, if you are writing a plugin that displays the number of comments on a post, you could use the _n function to display the correct form of the phrase based on the number of comments. Here’s an example usage code:

$comment_count = get_comments_number();
echo sprintf( _n( '%s comment', '%s comments', $comment_count ), number_format_i18n( $comment_count ) );

In this example, the first parameter is ‘%s comment’, which is the singular form of the phrase. The second parameter is ‘%s comments’, which is the plural form of the phrase. The third parameter is the variable $comment_count, which contains the number of comments.

The sprintf function is used to format the output of the _n function with the number of comments. The number_format_i18n function is also used to format the number with the correct decimal and thousand separators based on the current locale.

Overall, the _n function is a powerful tool in WordPress that allows developers to handle plural forms of phrases in a flexible and efficient way.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.