Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/daf218d7442c22d94cf1d1ddb061268b to your computer and use it in GitHub Desktop.
Save mishterk/daf218d7442c22d94cf1d1ddb061268b to your computer and use it in GitHub Desktop.
<?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
);
<?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
);
<?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