I working on some WordPress code with the WP Alchemy class, and I'm trying to recall the meta values used in a page template as a comma separated list. However when WP Alchemy Meta Boxes store the values into the domain, they aren't saved with delimiters nor spaces, so it's much like: onetwothreefourfive...

Here's what I have so far:

<?php $meta = get_post_meta(get_the_ID(), $custom_metabox->get_the_id(), TRUE); ?>
<li>Via: <?php foreach ($meta['g2m_via'] as $link) { ?><a href="<?php echo $link['g2m_via-link']; ?>">
<?php
$prefix = ', ';
$words = array();
$words[] = $link['g2m_via-title'];
$words = array_map("unserialize", array_unique(array_map("serialize", $words)));
for($i = 0; $i < count($words); $i++){ $fruitlist = implode(', ', $words); print_r($fruitlist); } 
?></a><?php } ?></li>

$link['g2m_via-title'] is simply the name of the link that is stored in the meta field, i.e. Link1 would be the name, google,,com would be the link (which is not important here, I have that working). The other variables are all there. The $prefix variable does nothing, it was meant to act as a separator, like: $val .= $prefix . '' $link['g2m_via-title']; . ''; however, it causes: Link1, Link 1,Link 2, Link 1, Link 2, Link 3.

So far with that code, I've gotten the closest to what I want:

Link1Link2Link3

But it needs to be: Link1, Link2, Link3, and so on without the comma on the last link title.

Output of var_dump($link):

array(2) { 
    ["g2m_via-title"]=> string(7) "JoyStiq" 
    ["g2m_via-link"]=> string(22) "joystiq.com"; 
}JoyStiq 
array(2) { 
    ["g2m_via-title"]=> string(9) "GrindGadget" 
    ["g2m_via-link"]=> string(16) "grindgadget.com"; 
} GrindGadget 
array(2) { 
    ["g2m_via-title"]=> string(13) "Engadget" 
    ["g2m_via-link"]=> string(13) "engadget.com"; 
} Engadget

What I WANT it to look like so ["g2m_via-title"] will stop duplicating:

array[1] { 
    ["g2m_via-title"]=> "JoyStiq" 
    ["g2m_via-link"]=> "joystiq.com"; 
}
array[2] { 
    ["g2m_via-title"]=> "GrindGadget" 
    ["g2m_via-link"]=> "grindgadget.com"; 
}
array[3] { 
    ["g2m_via-title"]=> "Engadget" 
    ["g2m_via-link"]=> "engadget.com"; 
}

3 of the countless other pieces of code that I've tried: http://pastebin.com/wa0R8sDw.

share|improve this question
1  
what is the value of $link['g2m_via-title']? – Jaitsu Aug 27 '11 at 12:19
1  
What does $link['g2m_via-title'] contain exactly? – Tatu Ulmanen Aug 27 '11 at 12:21
Oh sorry, $link['g2m_via-title'] contains the name of the link (i.e. Link 1). – David G2 Aug 27 '11 at 12:25
Can you give output of var_dump($link);. So I could know how it's stored. – GlitchMr Aug 27 '11 at 12:38
array(2) { ["g2m_via-title"]=> string(7) "JoyStiq" ["g2m_via-link"]=> string(22) "joystiq.com"; } JoyStiq array(2) { ["g2m_via-title"]=> string(9) "GrindGadget" ["g2m_via-link"]=> string(16) "grindgadget.com"; } GrindGadget array(2) { ["g2m_via-title"]=> string(13) "Engadget" ["g2m_via-link"]=> string(13) "engadget.com"; } Engadget ^Is that what you were looking for? – David G2 Aug 27 '11 at 12:42
show 7 more comments
feedback

1 Answer

up vote 1 down vote accepted

Assuming this data structure:

$links = array(
    array( 
        "g2m_via-title" => "JoyStiq",
        "g2m_via-link"  => "joystiq.com"
    ),
    array( 
        "g2m_via-title" => "GrindGadget",
        "g2m_via-link"  => "grindgadget.com"
    ),
    array( 
        "g2m_via-title" => "Engadget",
        "g2m_via-link"  => "engadget.com"
    )
);

This'll do:

$output = array();
foreach ($links as $link) {
    $output[] = sprintf('<a href="http://%s">%s</a>',
                        $link['g2m_via-link'],
                        htmlentities($link['g2m_via-title']));
}

echo join(', ', $output);

So will this in PHP 5.3+:

echo join(', ', array_map(function ($link) {
    return sprintf('<a href="http://%s">%s</a>',
                   $link['g2m_via-link'],
                   htmlentities($link['g2m_via-title']));
}, $links));
share|improve this answer
Actually, I still maintained the structure above, which is fine for now. But I'll definitely see if your code works now (the comma separation). – David G2 Aug 27 '11 at 15:12
You're amazing!!!!!!!!!!!!!!!!!! – David G2 Aug 27 '11 at 15:19
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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