Bird
Raised Fist0
Wordpressframework~10 mins

Advanced Custom Fields plugin in Wordpress - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the value of a custom field named 'subtitle' in a WordPress template.

Wordpress
<?php $subtitle = get_field('[1]'); ?>
Drag options to blanks, or click blank then click option'
Asubtitle
Btitle
Ccontent
Dauthor
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name like 'title' or 'content' instead of 'subtitle'.
Forgetting to wrap the field name in quotes.
2fill in blank
medium

Complete the code to display a custom field called 'price' inside a paragraph tag.

Wordpress
<p>Price: <?php [1]('price'); ?></p>
Drag options to blanks, or click blank then click option'
Aget_field
Bthe_field
Cthe_content
Dget_post_meta
Attempts:
3 left
💡 Hint
Common Mistakes
Using get_field without echo, resulting in no output.
Using unrelated functions like the_content.
3fill in blank
hard

Fix the error in the code to correctly check if a custom field 'featured' is true before showing a badge.

Wordpress
<?php if([1]('featured')): ?>
  <span class="badge">Featured</span>
<?php endif; ?>
Drag options to blanks, or click blank then click option'
Aget_field
Bhave_rows
Cthe_field
Dget_post_meta
Attempts:
3 left
💡 Hint
Common Mistakes
Using the_field inside if condition causing syntax errors.
Using have_rows which is for repeater fields, not simple true/false.
4fill in blank
hard

Fill both blanks to create a loop that lists all items in a repeater field named 'features'.

Wordpress
<?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; ?>
Drag options to blanks, or click blank then click option'
Afeatures
Bfeature_name
Cfeature
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names that do not match the repeater or subfield.
Not repeating the repeater field name in both have_rows calls.
5fill in blank
hard

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.

Wordpress
<?php
$ratings = [];
foreach($posts as [3]):
  $rating = [2];
  if($rating > 3):
    $ratings[[1]] = $rating;
  endif;
enforeach;
?>
Drag options to blanks, or click blank then click option'
A$post->ID
Bget_field('rating', $post->ID)
C$post
D$post->post_title
Attempts:
3 left
💡 Hint
Common Mistakes
Using post title instead of ID as key.
Not passing post ID to get_field.
Using wrong variable names in the loop.

Practice

(1/5)
1. What is the main purpose of the Advanced Custom Fields (ACF) plugin in WordPress?
easy
A. To add and manage extra custom fields without changing the database manually
B. To create new WordPress themes automatically
C. To speed up WordPress site loading times
D. To backup WordPress site data

Solution

  1. Step 1: Understand what ACF does

    ACF allows users to add custom fields easily without coding database changes.
  2. Step 2: Compare options

    Only To add and manage extra custom fields without changing the database manually describes adding and managing extra fields without manual database edits.
  3. Final Answer:

    To add and manage extra custom fields without changing the database manually -> Option A
  4. Quick Check:

    ACF purpose = add/manage custom fields [OK]
Hint: ACF is for custom fields, not themes or backups [OK]
Common Mistakes:
  • Confusing ACF with theme builders
  • Thinking ACF speeds up site loading
  • Assuming ACF handles backups
2. Which of the following is the correct way to retrieve a custom field value named subtitle using ACF in a WordPress template?
easy
A. the_field('subtitle');
B. fetch_field('subtitle');
C. get_field('subtitle');
D. custom_field('subtitle');

Solution

  1. Step 1: Identify the correct ACF function for retrieving values

    The function get_field() returns the value of a custom field.
  2. Step 2: Check other options

    the_field() echoes the value, not returns it; others are invalid functions.
  3. Final Answer:

    get_field('subtitle'); -> Option C
  4. Quick Check:

    Retrieve field value = get_field() [OK]
Hint: Use get_field() to get value, the_field() to print directly [OK]
Common Mistakes:
  • Using the_field() when value is needed for logic
  • Using non-existent functions like fetch_field()
  • Confusing echo and return functions
3. Consider this code snippet in a WordPress template using ACF:
$subtitle = get_field('subtitle');
if ($subtitle) {
  echo "

$subtitle

"; } else { echo "

No subtitle found

"; }

What will be the output if the custom field subtitle is empty or not set?
medium
A. PHP error will occur
B.

C. Nothing will be displayed
D.

No subtitle found

Solution

  1. Step 1: Understand the if condition

    The code checks if $subtitle has a value. If empty or not set, condition is false.
  2. Step 2: Check the else block output

    If false, it echoes "

    No subtitle found

    ".
  3. Final Answer:

    <h2>No subtitle found</h2> -> Option D
  4. Quick Check:

    Empty field triggers else output [OK]
Hint: Empty fields trigger else block output [OK]
Common Mistakes:
  • Assuming empty string prints empty <h2>
  • Expecting no output when field is empty
  • Thinking PHP error occurs on missing field
4. You have this code in your WordPress template:
echo get_field('price');

Sometimes it shows nothing and breaks the page layout. What is the best way to fix this issue?
medium
A. Remove the get_field() call completely
B. Check if the field has a value before echoing it
C. Use the_field() instead of get_field()
D. Change the field name to 'price_value'

Solution

  1. Step 1: Identify the problem cause

    Echoing an empty or null field can break layout if no fallback is provided.
  2. Step 2: Apply a check before echoing

    Use if (get_field('price')) to ensure value exists before output.
  3. Final Answer:

    Check if the field has a value before echoing it -> Option B
  4. Quick Check:

    Always check field value before output [OK]
Hint: Always verify field value exists before printing [OK]
Common Mistakes:
  • Removing field call instead of fixing output
  • Using the_field() which echoes without checks
  • Changing field name without updating code
5. You want to display a list of team members with their custom fields: name, role, and photo. Which approach correctly uses ACF to loop through a repeater field called team_members and display each member's info?
hard
A.
if (have_rows('team_members')) {
  while (have_rows('team_members')) {
    the_row();
    echo get_sub_field('name');
    echo get_sub_field('role');
    echo '';
  }
}
B.
foreach (get_field('team_members') as $member) {
  echo $member['name'];
  echo $member['role'];
  echo '';
}
C.
while (have_rows('team_members')) {
  echo get_sub_field('name');
  echo get_sub_field('role');
  echo get_sub_field('photo');
}
D.
if (get_field('team_members')) {
  echo get_field('team_members')['name'];
  echo get_field('team_members')['role'];
  echo get_field('team_members')['photo'];
}

Solution

  1. Step 1: Understand repeater field usage

    Repeater fields require have_rows() and the_row() to loop through rows.
  2. Step 2: Check sub field retrieval

    Use get_sub_field() inside the loop to get each subfield value.
  3. Step 3: Verify image field handling

    Image fields return an array; access ['url'] for the image source.
  4. Final Answer:

    Correct repeater loop with have_rows(), the_row(), get_sub_field(), and image url access -> Option A
  5. Quick Check:

    Repeater loop = have_rows() + the_row() + get_sub_field() [OK]
Hint: Use have_rows() and the_row() to loop repeater fields [OK]
Common Mistakes:
  • Using foreach on get_field() without have_rows()
  • Missing the_row() call inside loop
  • Not accessing image url from array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard