Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

right now I have two foreach arrays - one outputs the artist, the other a track title

I would like to combine both so there is a row for each artist and corresponding track

for example:

Row 1 - Artist 1 Track 1
Row 2 - Artist 2 Track 2

<?php // Get duplicate fields
$duplicate_field = get_post_custom_values('Artist', $post->ID);
foreach ( $duplicate_field as $key => $value ) {
echo $value . '<br />'; 
}
?>

<?php // Get duplicate fields
$duplicate_field = get_post_custom_values('Track', $post->ID);
foreach ( $duplicate_field as $key => $value ) {
echo $value . '<br />'; 
}
?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

if the artist names and track names are in matching order and the same in number then:

<?php
$artists = get_post_custom_values('Artist', $post->ID);
$tracks = get_post_custom_values('Track', $post->ID);
$i=0;
foreach ( $artists as $key => $value ) {
    $class = ( $i % 2 ) ? 'alternative' : ''; // modulus operator
    echo '<li class="' . $class . '">';
    echo $value . ' - '; 
    echo tracks[ $i ] . '</li>';
    $i++;
}
?>

otherwise if there is some other way they are matched up you need to figure that out in the loop.

share|improve this answer
    
worked perfectly! Is there a way to have every other <tr> to have a inline style such as <tr style="background-color: #ccc;"> –  Chad Jan 12 '12 at 6:47
    
I've edited the answer to add the alternative row class. (don't forget to mark the answer as correct and vote it up. I'm a point whore today ;) –  dwenaus Jan 12 '12 at 6:53
    
Thanks Again! Everything displays but the sort order is incorrect. I opened a separate thread with the issue here: wordpress.stackexchange.com/questions/38793/… –  Chad Jan 13 '12 at 7:13

Your Answer

 
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.