the_field vs get_field
-
I’m learning WP using Local by Flywheel environment and using this plugin for the first time so enlightenment please before I tear my hair out.
Note, I?have set the WP date format the same as the ACF plugin date format i.e. ‘d/m/y’.
Code below sort of works. If I’m reading this right since the_field has an embedded echo it should print out the value of ‘event_date’ as a string argument
$eventDate = new DateTime(the_field('event_date')); echo $eventDate->format('M');
When I say sort of I meant it does not error out but produces this: the event date + publication month which is not what I want or expect at all. The only way to get the event date in the form of the month is to use this code….
$eventDate = new DateTime(get_field('event_date', false, false)); echo $eventDate->format('M');
Code below does not. Again if I’m reading this right this returns a value which contains the date held in ‘event_date’. I really can’t see what the problem is here.
$eventDate = new DateTime(get_field('event_date')); echo $eventDate->format('M');
ERROR RETURNED IS:
Upcoming Events
FATAL ERROR: UNCAUGHT EXCEPTION: DATETIME::__CONSTRUCT(): FAILED TO PARSE TIME STRING (19/09/2019) AT POSITION 0 (1): UNEXPECTED CHARACTER IN /APP/PUBLIC/WP-CONTENT/THEMES/MYTHEME/FRONT-PAGE.PHP:34According to the documentation: the_field();
Intuitive and powerful, this function can be used to output the value of any field from any location. Please note this function is the same as echo get_field();Additionally when I used this on another part of my page as a test it did not print anything out?
<h1><?php the_field('event_date'); ?></h1>
- The topic ‘the_field vs get_field’ is closed to new replies.