Bird
0
0

You want to display a custom field 'event_date' formatted as 'F j, Y' (e.g., March 5, 2024) for post 50. Which code snippet correctly achieves this?

hard📝 component behavior Q8 of 15
Wordpress - Custom Fields and Meta Data
You want to display a custom field 'event_date' formatted as 'F j, Y' (e.g., March 5, 2024) for post 50. Which code snippet correctly achieves this?
A$date = get_post_meta(50, 'event_date', true);<br>echo date('F j, Y', strtotime($date));
B$date = get_post_meta(50, 'event_date');<br>echo date('F j, Y', $date);
Cecho get_post_meta(50, 'event_date', true)->format('F j, Y');
D$date = get_post_meta(50, 'event_date', false);<br>echo date('F j, Y', strtotime($date));
Step-by-Step Solution
Solution:
  1. Step 1: Retrieve single string date

    Use get_post_meta with true to get string date.
  2. Step 2: Format date string

    Use strtotime to convert string to timestamp, then date() to format.
  3. Step 3: Check other options

    B misses true param and passes array to date; C treats string as object; D uses false returning array.
  4. Final Answer:

    Option A correctly formats and displays the date -> Option A
  5. Quick Check:

    Get string date + strtotime + date() = correct format [OK]
Quick Trick: Use strtotime() on meta string before date() formatting [OK]
Common Mistakes:
  • Passing array to date()
  • Treating string as object
  • Omitting true for single value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes