• The code works on the admin’s profile but not to other user profile.. im using user front-end pro plugin..

    this is the error that i get if the user profile is not admin’s profile
    Warning: Invalid argument supplied for foreach() in /mypath/author.php on line 109

    this is line 109
    foreach ($images as $attachment_id) {

    complete code

    <?php
    $images = get_user_meta( get_the_author_meta( 'ID' ), 'personal_playlist' );
    if (count($images) > 0) {
    $array = array();
        foreach ($images as $attachment_id) {
            $array[] = $attachment_id;
        }
    
    $ALL = implode(',', $array);
    echo do_shortcode('[playlist tracklist="true" style="dark" images="true" artists="false" ids="' . $ALL . '"]');
    }
    
     ?>

    i tried asking for support in their site but they dont give support for custom modifications.. please help me out.

Viewing 8 replies - 1 through 8 (of 8 total)
  • tsagita

    (@lemurianisindonesia)

    because variable images is null, make sure user profile is not admin have “personal_playlist” user meta

    The images variable may not be NULL. as seen at the codex page, if you add the name, the value will be what’s stored in the field, so that could be anything (string, array, integer, object, etc).

    The way to find out what that value is, is to do something like this:

    $images = get_user_meta( get_the_author_meta( 'ID' ), 'personal_playlist' );
    echo "<pre>";
    var_export( $images );
    echo "</pre>";

    That way you’ll know what value you’re getting back, and you’ll be able to deal with it accordingly.

    Thread Starter Metal_13

    (@metal_13)

    thanks for the reply.. @catacaustic i got this on

    admin account

    array (
      0 => '12818',
      1 => '12819',
      2 => '12820',
      3 => '12821',
      4 => '12822',
      5 => '12823',
    )

    other user account
    false

    is this a plugin bug?

    No. It’s pretty much what you expect to get back. When the value exists, it gives back the array. If it doesn’t exist, you get false back to indicate the failure.

    The good news is that there’s an easy way to work in with this…

    $images = get_user_meta( get_the_author_meta( 'ID' ), 'personal_playlist' );
    if (is_array ($images) && count($images) > 0) {
        $array = array();
        foreach ($images as $attachment_id) {
            $array[] = $attachment_id;
        }
    
        $ALL = implode(',', $array);
        echo do_shortcode('[playlist tracklist="true" style="dark" images="true" artists="false" ids="' . $ALL . '"]');
    }

    All that I’ve added in there is the is_array() check, and that will only execute that loop if the returned value actually is an array.

    Thread Starter Metal_13

    (@metal_13)

    the code doesn’t work too.. playlist is only showing in the admin account.. this is frustrating..

    any ideas why this is happening?

    So what de-bugging have you done? What do you get when you do this:

    $images = get_user_meta( get_the_author_meta( 'ID' ), 'personal_playlist' );
    echo "<p>Images is array: '".is_array ($images)."'; Image count: '".count ($images)."'</p>";

    As for only showing in the admin account, there’s nothing there that is admin-specific at all, so there’s nothing in that code that could suggest why that might be happening, so it’s most likely somewhere else.

    Thread Starter Metal_13

    (@metal_13)

    im getting this
    Images is array: '1'; Image count: '0'

    i tried random debugging i cant remember.. i always end up with blank..

    That means that you are getting a result, but there’s no images. As there’s no images there’s nothing to display. That’s why there’s nothing showing.

    There’s only two things that can be wrong here. First, you might not be storing the images correctly. That’s the first thing that I’d check, so just verify that your system is saving the value as you’d expect it to. Second, check how you’re getting the value back. If the value is being stored correctly then retrieving it should be getting the right values back, and if it’s not there’s some issue with how you’re getting then again.

    This will need more de-bugging on your part because we can only see that small bit of code, and there’s nothing obvious in there that is wrong, so from that the best chance is that the error is somewhere else in your system.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Can someone tell me whats wrong with my code (foreach error)’ is closed to new replies.