mercredi 19 octobre 2016

If statement for True/False user field wont work

I have created a ACF True/False field which is attached to my users. This is used as a access level option, so if the option is True they will see a link in the menu, if false, they will see a fallback link.

Below is the code iv added to my functions.php where iv created a shortcode: “customLink” which is then added to my menu link. Below is what i have so far, which hopefully shows what im trying to do. If the users role us ‘my_custom_role’, then to get the ACF value, and if true, show one link, and if false show another.

add_shortcode('customLink', 'cm_link');
function cm_link($atts) {
$atts = shortcode_atts(array('title' => 'Custom Link', 'fallbackurl' => '#'), $atts, 'customLink');
$advcontent = get_field('acf_field', $current_user->ID);
        $user = wp_get_current_user();
        if (!empty($user->roles) && is_array($user->roles)) {
            foreach ( $user->roles as $role ) {
                if ($role=='my_custom_role') {
                    if ($advcontent == 'true') {
                        return "<a href='/my-link/'>".$atts['title']."</a>";
                    }
                    if ($advcontent == 'false') {
                        return "<a>".$atts['title']."</a>";
                    }
                }
            }
        }        
    }

Iv also tried the below, but no luck:

add_shortcode('customLink', 'cm_link');
function cm_link($atts) {
$atts = shortcode_atts(array('title' => 'Custom Link', 'fallbackurl' => '#'), $atts, 'customLink');
$advcontent = $_POST['acf']['field_58061ec5608bd'];
if ($advcontent == 'true' ) {
    return "<a href='/my-link'>".$atts['title']."</a>";
} else {
    return "<a>".$atts['title']."</a>";
    }      
}

Aucun commentaire:

Enregistrer un commentaire