Support

Account

Home Forums General Issues show acf content by user role

Solved

show acf content by user role

  • Hello,

    i use extension for ACF PRO Advanced Custom Fields: User Role Selector, so now i can add field where i can select which roles can see this content on frontpage.

    Can someone help me with php, how i can check and control content for display … user can have one role or two so:

    I need code for:

    if user have role admin + editor -> show this , if no -> show this

    i using simply code, but its work only for one role:

    <?php
                                      
    $values = get_sub_field('roles_acf_field');
    if($values)
    {
    	foreach($values as $value)
    	{
            array( $value );
    	}
    }                                  
                                      
    
    if (in_array(administrator, $values))
      {
      echo "Match found";
      }
    else
      {
      echo "Match not found";
      }                               
    ?> 

    thank you so much

  • Ok problem solved:

    <?php

    <?php
                                      
    $values = get_sub_field('skupiny');                              
      
    $current_user = wp_get_current_user();
    if ( !($current_user instanceof WP_User) )
       return;
    $roles = $current_user->roles;  //$roles is an array                                                               
                                      
    if (array_intersect($roles, $values))
      { ?>
    
    /// content for selected roles goes here ///      
       
    <?php
     }
    else 
    ?> 
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘show acf content by user role’ is closed to new replies.