Support

Account

Home Forums General Issues Values from subfield into php array code

Solved

Values from subfield into php array code

  • Hello,

    i use flexibile content with few blocks, and each block contain user field with multichoice … i need block display only for selected users.

    I use this code and i do not know how i can echo ID of all selected users into array as values, i need $people = array(id1, id2, 3, 4, 5 …);

    <?php
    $kto = the_sub_field('zobrazit_pre');                                  
                                   
    $people = array($kto);
    
    if (in_array($current_user->ID, $people))
      {
      echo "Match found";
      }
    else
      {
      echo "Match not found";
      }
    ?>

    If i echo variable $kto i can get ID of selected user 1,2,3 … but how i can echo this IDs in array? This do not work for me and i do not know how make it 🙁 Many thanks for help

  • Problem solved

    <?php 
    	//Fetch users and set our boolean to true per default
    	$users = get_sub_field('zobrazit_pre');
    	$show_content = true;
    	if ( !is_user_logged_in() && $users) {
    		//If the visitor isn't logged in and we have users they should not be able to see the content
    		$show_content = false;
    	}elseif( is_user_logged_in() && $users ){
    		//Okay so they're logged in and we have users we need to check against
    		$current_user = wp_get_current_user();
    		//assume they should not see it (just in case)
    		$show_content = false;
    		foreach( $users as $user ){
    			//Loop through each user array from the ACF field
    			if( $user['ID'] == $current_user->ID ){
    				//Alright they are in the clear! Lets set our boolean to true and break early!
    				$show_content = true;
    				break;
    			}
    			
    		}
    	}
     ?>
    
    <?php if( $show_content ){ ?>
    
     //////// content go here
    
    <?php } ?> 
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Values from subfield into php array code’ is closed to new replies.