Complete the code to get the value of a custom field named 'subtitle' in a WordPress template.
<?php $subtitle = get_field('[1]'); ?>
The get_field function retrieves the value of the custom field by its name. Here, 'subtitle' is the correct field name.
Complete the code to display a custom field called 'price' inside a paragraph tag.
<p>Price: <?php [1]('price'); ?></p>
get_field without echo, resulting in no output.the_content.the_field echoes the value of the custom field directly, which is suitable for displaying inside HTML.
Fix the error in the code to correctly check if a custom field 'featured' is true before showing a badge.
<?php if([1]('featured')): ?> <span class="badge">Featured</span> <?php endif; ?>
the_field inside if condition causing syntax errors.have_rows which is for repeater fields, not simple true/false.get_field returns the value of the field, which can be checked in an if statement. the_field echoes the value and cannot be used in conditions.
Fill both blanks to create a loop that lists all items in a repeater field named 'features'.
<?php if(have_rows('[1]')): ?> <ul> <?php while(have_rows('[1]')): the_row(); ?> <li><?php the_sub_field('[2]'); ?></li> <?php endwhile; ?> </ul> <?php endif; ?>
The repeater field name is 'features'. Inside the loop, the_sub_field uses the subfield name, here 'feature_name', to display each item.
Fill all three blanks to create an associative array that maps each post ID to its custom field 'rating' if the rating is above 3.
<?php $ratings = []; foreach($posts as [3]): $rating = [2]; if($rating > 3): $ratings[[1]] = $rating; endif; enforeach; ?>
get_field.This code creates an associative array mapping each post's ID to its 'rating' custom field, filtering only ratings above 3.