Last active
January 30, 2022 02:22
Snippets for the article: https://hookturn.io/singular-or-pluralised-string-wordpress/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Use an ACF field value to pull the number from the database. | |
$number_of_items = (int) get_field('my_numerical_field', $post_id ); | |
$string = sprintf( | |
_n( | |
'%d item in stock', | |
'%d items in stock', | |
$number_of_items | |
), | |
$number_of_items | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Combine it with sprintf() to dynamically show the exact | |
// number of items in the resulting string. | |
$string = sprintf( | |
_n( | |
'%d item in stock', | |
'%d items in stock', | |
$number_of_items | |
), | |
$number_of_items | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Display a string in either singular or plural form depending | |
// on the number of items. _n() is a translation function so this | |
// will also translate where a translation is available. | |
$string = _n( | |
'item', | |
'items', | |
$number_of_items | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment