• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Damien Carbery - Website Development

Getting You Online & Keeping You There

Getting You Online & Keeping You There Get Started
  • Home
  • Services
  • Maintenance Plans
  • Portfolio
  • Blog
  • About Me
  • Contact
You are here: Home / Website Development / Populate ACF Select field with menu names

Populate ACF Select field with menu names

Published: December 12, 2022

Populate ACF Select field with menu names
Use the acf/load_value filter to load values to a Select field.

I recently saw the code for a custom ACF Pro block that rendered a menu. The field group used a text field for the menu name. I felt that asking the user to enter the menu name was error prone so I experimented with changing the text field to a select field, populated with a list of available menus.

Populate a select field

With the acf/load_field filter I can add to or completely replace the drop down values set in the Field Group definition. I use acf/load_field/type=select so that the filter function only runs on select fields. If you have multiple field groups or multiple select fields you can add additional checks to confirm you are modifying the right field e.g. check the field key or name.

Get menus and their IDs

Getting the menus was easier than I expected: wp_get_nav_menus() returns an array of menu objects. I loop through these to get the menu name and ID and set the select field’s choices to this list. Using the menu ID allows for the menu name to be changed without impacting any chosen menu.

Output the menu

The code that inspired this post used a ACF block. To keep the code here short and simple I chose to simply render the chosen menu at the end of the content of the post (via the_content filter). Using it in a block would be very similar (get_field() and wp_nav_menu()).

If the chosen menu does not exist WordPress will return the first non-empty menu. I want it to return nothing and a comment in the documention from 2014 said that passing a bogus theme_location is the only way to achieve this. This is why I added theme_location and fallback_cb arguments to the wp_nav_menu() call.

Screenshots

The menus under Appearance/Menus
Choosing the menu in the ACF field group

The code

Related Posts

Flip Card block with ACF Pro

Create a custom Gutenberg block for 3D hover animation

Use CMB2 to create a carousel

Add photos to multiple groups and randomise group order in a carousel

Comment Block for Gutenberg

A comment block that is only shown when editing content. It is not displayed on front end.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Blog Categories

  • Being Green
  • Food
  • Grammar
  • Just Strange
  • Laziness
  • Personal
  • Portfolio
  • Race Reports
  • Shudder
  • Typo
  • Website Development
  • WooCommerce Tips
  • Facebook
  • Twitter

Copyright © 2013–2023 Damien Carbery · Using Genesis Framework · Privacy Policy