Forums

Display custom fields one value multiple times (4 posts)

  1. gdesign
    Member
    Posted 4 years ago #

    Dear wordpress users,

    I have been teasing my brain over this all evening but simply can not find an answer.

    I have set up a custom field with the value "portdetail" I use this value
    multiple times I would like to display it multiple times to.

    I have this code;

    <?php
      $custom_fields = get_post_custom();
      $my_custom_field = $custom_fields['portdetail'];
      foreach ( $my_custom_field as $key => $value )
      echo "<li><img src='http://gdesignstudio.nl/wp-content/uploads/" . $value . "' /><li/>"; ?>

    This really works like a charm if I wouldt get this error;

    Warning: Invalid argument supplied for foreach() in /usr/home/deb10155/domains/gdesignstudio.nl/public_html/wp-content/themes/GD/portfolio.php on line 36

    I'm using this code within a "query_posts" but just cant find a way to get rid of the error.

    Thank you all in forward for your answers.

    greetings,
    Hans van Wijk

  2. Alex Cragg
    Member
    Posted 4 years ago #

    I just want to check that I understand what you are trying to do. You said

    I use this value
    multiple times I would like to display it multiple times to
    and that you are using the loop. Does this mean that you want to display the custom field value for each post displayed in that loop, and have that custom value be relative to the post?

    Do you mean that portdetail is the 'key', or the 'value'?

    Once I'm clear on that I might be able to help.

    Alex

  3. gdesign
    Member
    Posted 4 years ago #

    Sorry, portdetail is the key and I want to display it multiple times in 1 post to show a list of images.

  4. Alex Cragg
    Member
    Posted 4 years ago #

    OK, I haven't tested this, but I hope that it works! :-)
    Its a lot simpler than before:

    <?php
    $my_custom_field = get_post_custom_values('portdetail');
    foreach ( $my_custom_field as $key => $value ) {
    echo "<li><img src='http://gdesignstudio.nl/wp-content/uploads/" . $value . "' /><li/>" ;
    } ?>

    get_post_custom_values($key) gets a list of the values for the given key on the post you are viewing, and stores them in an array.

    Let me know if that works...

Topic Closed

This topic has been closed to new replies.

About this Topic