Prepend Letter Before & Append Letter After Custom Field Value

This code shows you how to automatically add a letter or letters before the dynamic value from a custom field as well as add letter(s) after the value.

$time       = get_post_meta( get_the_ID(), 'totalTime', true );
$PTM        = 'PT' . $time . 'M';
$time_PTM   = $time  ? $PTM : 'PT5M';

In this example, the letters PT are prepended before the custom field value and the letter M is appended after.

This enables you to use a custom field to add the value as a number so the result would be something like PT8M which relates to 8 mins.

If no value is added, a default value of PT5M is output as seem in the 3rd line of code which uses a ternary operator.

Used for schema totalTime as seen in the 1st example of this JSON script.

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.