User meta multiple value/ array output

Home Forums Plugin Support User meta multiple value/ array output

This topic contains 2 replies, has 2 voices, and was last updated by  MH Russel 1 year, 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #9004

    MH Russel
    Member

    I’m using this code in author.php

    <?php
    $curauth = (isset($_GET[‘author_name’])) ? get_user_by(‘slug’, $author_name) : get_userdata(intval($author));
    ?>

    This code to get output
    <p><?php echo $curauth->memb_sectors; ?></p>

    But when using multiple select options then we are getting text “array” not a result. How to fix that. Could you help me?

    #9005

    sourov amin
    Moderator

    Hello,

    All the values of the multi-select field are stored in the array data type. That is why you are getting the “array” text. To get each data of a multi-select field you can use a simple loop like below:

    foreach( $curauth->memb_sectors as $key=>$value ) {
        echo $value;
        echo '<br>';
    }

    Also, with print_r() or var_dump() instead of echo, you can display the array data in a readable format.

    Thanks.

    #9006

    MH Russel
    Member

    Thank you for your help, I’m not good in code. Could you help me to apply conditional logic?

    Now showing array when 1 value.

    We need to use a conditional statement. If more than a sector else value for a sector
    I tried some code but not working

    Also, I need to know is there a way to use fields like post Tags/ user tag. Please let me know the process if it’s possible.

    foreach($curauth->memb_sectors as $key=>$value) {
    static $i = 0;
    if ($i >= 2) {
    echo $value;
    } else {
    echo $curauth->memb_sectors;
    }
    $i++;
    }

    • This reply was modified 1 year, 2 months ago by  MH Russel.
    • This reply was modified 1 year, 2 months ago by  MH Russel.
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.