Support » Fixing WordPress » Problem with the custom fields (coding issue)

  • Resolved creativecoder


    I am trying to pull a video from youtube and post it OVER the <?php the_title(); ?> area. This is the code I’m using for the custom fields (I have this code in single.php) in order to do that:

       $custom_fields = get_post_custom(0);
       $my_custom_field = $custom_fields['media'];
       foreach ( $my_custom_field as $key => $value ) {
       echo $value;

    Now this is fine, it works…however if I do not actually provide a custom value (because I just don’t want a video on some posts) – it shows this error:

    Warning: Invalid argument supplied for foreach() in /public_html/ on line 12.

    So essentially what is happening is it has a problem with this line of code when I don’t supply a video for the post:

    foreach ( $my_custom_field as $key => $value ) {

    Any ideas on how I can not provide a video on some posts and eliminate the error?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Why not install’s excellent Get Custom plugin and add the url for each page or post that has one as a custom field?

    Then you’d use one of their functions (echo c2c_get_custom($key,$before,$after,$none,$between); (if in the loop)

    or the get_recent_custom() (if not in the loop)


    The plugin allows you to put html content before and after the value of the key ONLY if the key exists AND you’ve put somethine between the ticks ” for $between param – like a space or anything other than nothing

    I find this plugin an absolute must for retrieving custom fiels — it is the easiest to return and you can insert variables or dynamic code in it if you have adequate php skills.

    Thank you so very much! It was as easy as installing the plugin and defining my custom field’s ID in the following line of code:

    <?php echo c2c_get_custom('media'); ?>

    Thanks again!

    It’s a wonderful plugin – I use it with just about every site I build in WordPress. And you can run it inside or outside of the loop, which when using wp as a cms, is incredibly important.

    the c2c_get_recent_custom() function is powerful – you can use it to define any number of sitewide/global fields in your homepage or a separate (non-content) page to allow a client to be able to edit stuff found in non-page or non-post files (such as a sidebar or footer or other included file) as long as those fields use unique names.

    It truly is my #1 most favorite plugin that makes my workflow easy. I build totally custom markup and just use either default WordPress logic to power the site or with the aid of the c2c functions, using custom fields (and my own coding) to create data-driven sites that keep the client away from the html and css markup. I’ve even used the get_recent to allow the client to edit basic colors of key elements in the css… via a custom field in a page named “site style” that isn’t shown in any menu.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with the custom fields (coding issue)’ is closed to new replies.