I’d like to add page specific custom CTAs in the main menu of my site. I have created a Default CTA field and a Custom CTA repeater field. In the custom CTA repeater field the user can select the custom CTA (link field) and the pages (multiple post objects) on which this custom CTA will show. On the rest of the pages, the Default CTA will show.
I am following the ACF documentation for adding fields to WordPress Menus. But when I try to loop through the Custom CTA repeater field, I am getting these errors:
<b>Notice</b>: Array to string conversion in <b>/Users/prashansapoddar/documents/wp-sites/realease/wp-content/plugins/advanced-custom-fields-pro/includes/api/api-template.php</b> on line <b>357</b><br />
<br />
<b>Notice</b>: Array to string conversion in <b>/Users/prashansapoddar/documents/wp-sites/realease/wp-content/plugins/advanced-custom-fields-pro/includes/api/api-value.php</b> on line <b>42</b><br />
So far, my code is this:
add_filter('wp_nav_menu_items', 'acf_wp_nav_menu_ctas', 10, 2);
function acf_wp_nav_menu_ctas( $items, $args ) {
// get menu
$menu = wp_get_nav_menu_object($args->menu);
// modify primary only
if( $args->theme_location == 'primary-menu' ) {
// default CTA vars
$default_cta = get_field('nav_cta', $menu);
$default_cta_link = $default_cta['url'];
$default_cta_text = $default_cta['title'];
// custom CTA vars
$custom_ctas = get_field('custom_ctas', $menu);
if( have_rows( $custom_ctas ) ) {
while( have_rows( $custom_ctas )) : the_row();
$selected_page = get_sub_field('selection');
if ( $selected_page ) {
foreach($selected_page as $post_object) {
$custom_cta = get_sub_field('cta', $menu);
$custom_cta_link = $custom_cta['url'];
$custom_cta_text = $custom_cta['title'];
if(is_page($post_object->ID))
$nav_cta = '<li class="site__header-cta"><a href="'.$custom_cta_link.'" class="button button--secondary">'.$custom_cta_text.'</a></li>';
// append html
$items = $items . $nav_cta;
}
}
endwhile;
} else {
$nav_cta = '<li class="site__header-cta"><a href="'.$default_cta_link.'" class="button button--secondary">'.$default_cta_text.'</a></li>';
// append html
$items = $items . $nav_cta;
}
}
// return
return $items;
}
Any help would be much appreciated. I would also like some guidance on how I could check for the page selected for the custom CTA (should I use WP’s is_page() functionality)?
Thanks
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 ACF & ACF PRO 6.0.7 are now available.
— Advanced Custom Fields (@wp_acf) January 18, 2023
✨This release contains bug fixes and improvements while we continue to work on the next major release of ACF.https://t.co/wQgAOpwmUI
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.