I have this bit of code for displaying the value of links key of my custom fields.

$links = get_post_meta($post->ID, 'links', false);
foreach($links as $link)
    echo '<div class="first-link bottom-dotted"><p><a href="">'.$link.'</p></div></a>';

What I need to do is insert the value of another custom field called say URLs for each a href tag in my foreach statement.

Any idea how to achieve this?

I'm not sure I can do something like this:

$links = get_post_meta($post->ID, 'links', false);
$URLs = get_post_meta($post->ID, 'URLs', false);
foreach($links as $link) || foreach($URLs as $URL) {
    echo '<div class="first-link bottom-dotted"><p><a href="'.$URL.'">'.$link.'</p></div></a>';
  • What is the difference between 'links' and 'URLs'? The current code, however, makes no sense. Do you want to reference each of the 'URLs' with the current 'link', or do you want to echo for each 'link' a link (i.e., <a> tag) with each of the 'URLs'...? – tfrommen Mar 5 '13 at 23:06
  • How are 'links' associated with 'URLs'? – Chip Bennett Mar 5 '13 at 23:18
  • By links i mean anchor text and by url I mean the actual url. SO what i want is to add the anchor text using one custom field and the url for that anchor using another custom field. Makes sense? – Sasha Mar 5 '13 at 23:33
  • This is a bad design, they should be stored together. There is no way to just guess which URL matches which link, you can not even be sure that the number of them is the same. – Mark Kaplun Mar 5 '13 at 23:51

I don't know if you can count on the order of both custom fields to be equal when returned from get_post_meta(), but if you can, you could use:

$links = get_post_meta($post->ID, 'links', false);
$URLs = get_post_meta($post->ID, 'URLs', false);
for ( $i = 0; $i < count( $links ); $i++ ) {
    if ( empty( $URLs[$i] ) ) {
        $URLs[$i] = '#';
    echo '<div class="first-link bottom-dotted"><p><a href="'.$URLs[$i].'">'.$links[$i].'</a></p></div>';
  • You're welcome. I just corrected the placement of the </a> tag (as it was first outside the </p>), in case you copied/pasted the code :-) – Vivienne Mar 6 '13 at 0:12
  • PS. You might want to accept my answer as 'accepted answer' and close this question. – Vivienne Mar 6 '13 at 0:20
  • @markkaplun is correct. The way that Alex is storing data is bad design and this solution does nothing to correct or compensate for that. – s_ha_dum Mar 6 '13 at 1:08
  • I actually agree with @markkaplun (and @s_ha_dum), the above is only useful if you can be sure about the order of both values when using get_post_meta(), as they have no link between them. Another solution would be to store both values in the 'links' custom field as link@@url, and then split on @@ when getting the value with get_post_meta() (for instance). That way, you're always sure about getting the correct link with the correct URL. – Vivienne Mar 6 '13 at 7:09
  • I would also agree with all of you - only that I have no idea how to do it - i'm really no programmer just struggling to learn stuff. Can anyone help me with a 'proper' solution (code would be very useful) - thank you in advance. – Sasha Mar 6 '13 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.