Support

Account

Home Forums Add-ons Options Page get_field not working for options page

Helping

get_field not working for options page

  • I’ve seen other people report similar issues but thought to start another post in case it’s a different issue.

    Basically get_field() is returning a false value when it should be returning data. I’ll paste all of my code below, including the code that is creating the option field.

    ananda_debug( get_field( 'ancestor_urls', 'option' ) ); is an admin-only debug function, and it outputs false no matter what data is in the option panel.

    It’s a custom option panel and I’m on the latest version of WordPress running Multisite.

    Thanks for any help, I’m not able to move ahead with this project.

    BTW, Advanced Custom Fields is WONDERFUL. I use it for almost every new project now.

    
    
    // Add an options page for choosing parent in navigation
    if ( function_exists( 'acf_add_options_sub_page' ) ) {
       acf_add_options_sub_page( 'Navigation' );
    }
    
    if(function_exists("register_field_group"))
    {
    	register_field_group(array (
    		'id' => 'acf_navigation',
    		'title' => 'Navigation ',
    		'fields' => array (
    			array (
    				'key' => 'field_529095c3cdfcb',
    				'label' => __('Ancestor URLs'),
    				'name' => 'ancestor_urls',
    				'type' => 'repeater',
    				'instructions' => __('For each parent / grandparent / etc. of the blog, add a complete URL, including a slash at the end, that matches the navigation items you want to highlight.'),
    				'sub_fields' => array (
    					array (
    						'key' => 'field_52909683cdfcc',
    						'label' => __('URL'),
    						'name' => 'url',
    						'type' => 'text',
    						'instructions' => __('For example, http://www.ananda.org/meditation/'),
    						'column_width' => '',
    						'default_value' => 'http://',
    						'placeholder' => '',
    						'prepend' => '',
    						'append' => '',
    						'formatting' => 'none',
    						'maxlength' => 255,
    					),
    				),
    				'row_min' => 0,
    				'row_limit' => '',
    				'layout' => 'table',
    				'button_label' => 'Add URL',
    			),
    		),
    		'location' => array (
    			array (
    				array (
    					'param' => 'options_page',
    					'operator' => '==',
    					'value' => 'acf-options-navigation',
    					'order_no' => 0,
    					'group_no' => 0,
    				),
    			),
    		),
    		'options' => array (
    			'position' => 'normal',
    			'layout' => 'no_box',
    			'hide_on_screen' => array (
    			),
    		),
    		'menu_order' => 0,
    	));
    }
    
    // New way of doing this 
    function ananda_blogs_highlight_ancestors( $classes, $item ) {
    		
    	ananda_debug( get_field( 'ancestor_urls', 'option' ) );
    	
    	if ( get_field( 'ancestor_urls', 'option' ) ) {
    	
    		while( has_sub_field( 'ancestor_urls', 'option' ) ) {
    	 
    			if ( $item->url == get_sub_field( 'url', 'option' ) ) {
    	    		$classes[] = 'current-menu-ancestor';
    	    	}
    				 
    		}
    
    	}
    	
    	return $classes;
    	
    }
    
    if ( current_user_can( 'manage_network' ) ) {
    	add_filter('nav_menu_css_class', 'ananda_blogs_highlight_ancestors', 10, 2 );
    }
    
  • Hi @nabha

    Thanks for teh bug report. I am unable to reproduce the error on my end. Can you please remove some variables and simply print out the value in your functions.php file like so:

    
    echo '<pre>';
    		print_r( get_field('ancestor_urls', 'option') );
    	echo '</pre>';
    	die; 
    

    Thanks
    E

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘get_field not working for options page’ is closed to new replies.