WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] widget spitting out values as arrays instead of single items (6 posts)

  1. michellekoen
    Member
    Posted 4 years ago #

    Hi,
    I'm building my first widget for my husband's record label. I'm getting information from two custom fields and using it to display some flash widgets from Reverb Nation.

    The custom fields are artist ID and media type. There is only a single instance of each on each post.

    For some reason my widget is returning the values from the widget control panel as arrays rather than single values. I can get the widget to 'work' if I use get_post_custom_values and then do a foreach to split out the array. However this spits me all sorts of errors when error testing is on.

    How do I get the widget to return single values, so that I can use get_post_meta. (like where I'm trying to set a value for $newlink and $newreverb.

    This is the first widget I've written and the first PHP I've touched in about 6 years.

    This is the code
    [Code moderated as per the Forum Rules. Please use the pastebin]

    Thanks Community

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    You need to set the third parameter of get_post_meta to true in order to return a single value, default is false (an array of values).

    http://codex.wordpress.org/Function_Reference/get_post_meta

    Example.

    $my_field = get_post_meta( $post->ID, 'your-field-name', true );
    if( $my_field )
       // Do whatever
  3. michellekoen
    Member
    Posted 4 years ago #

    Thanks Mark

    Ooops didn't know about pastebin
    Here's the code

    Lines 80 and 81 are using the format you describe above and returning nothing due true referring to 'is it a single item or an array?'

    For some reason it's returning as an array, if I set TRUE then it spits out nothing. If I set FALSE then I still have the problem that it's an array.

    What I need to know is WHY it's making the results in my widget an array (with only 1 item) and how I can fix it.

    Michelle

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Not sure i follow quite what it is the widget is doing but i see a few problems in your code.. so i'll highlight them so you can make corrections.

    Your calls to get_post_custom_values will always give you an array, that's what that function is for, this will do pretty much the same as get_post_meta except there's no option for returning a singular value (it's always an array or nothing). Use get_post_meta as you have a few lines down if you want only a single value returned.

    First loop..

    foreach ( $link as $RNMkey => $RNMvalue ) {    $cvalue=$RNMvalue;   }

    $cvalue will only ever be the value of the last array item, since you're assigning the same variable with each iteration of the foreach loop (the last result will be what's stored in $cvalue). If this is intended behaviour, you could do this instead..

    // Last item in array
    $cvalue = end( $link );
    
    // or
    
    // If you wanted the first item in array
    $cvalue = current( $link );

    Moving on, inside the next foreach loop are various lines like so..

    $RNWidgetCheck= Music;
    
    ..
    
    $RNWidgetCheck= Video;

    Your values should have quotes around them..

    Eg.

    $RNWidgetCheck = 'Video';

    I'm surprised the lines above didn't cause PHP to throw up a few errors.

    Could quite possibly be other problems, but those are the ones that stand out on first glance, so i hope that helps.. ;)

  5. michellekoen
    Member
    Posted 4 years ago #

    Thanks Mark!

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're welcome.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic