I have the following piece of code, which is based on the solution to this question:
<?php
$game_titles = get_terms('game');
foreach($game_titles as $game_title) {
wp_reset_query();
$args = array('post_type' => 'release',
'tax_query' => array(
array(
'taxonomy' => 'game',
'field' => 'slug',
'terms' => $game_title->slug,
),
),
);
unset($game_titles);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>' . $game_title->name . '</h2>';
echo '<ul>';
unset($game_title);
unset($args);
while($loop->have_posts()) : $loop->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
echo '<p style="color:grey;font-size:11px;margin:0;">(' .
$number = intval(get_post_meta(get_the_ID(), 'number', true));
if($number > 1){
echo $number . 'x';
}
$format_value = get_post_meta(get_the_ID(), 'format', true);
foreach($format_value as $key => $val){
echo $val . ', ';
}
unset($val);
$version_value = get_post_meta(get_the_ID(), 'version_desc', true);
foreach($version_value as $key => $val){
echo $val . ', ';
}
unset($val);
$color = get_post_meta(get_the_ID(), 'vinyl_color', true);
if($color != 'Black'){
if($color != ''){
echo ' / ' . $color;
}
}
$country_value = get_post_meta(get_the_ID(), 'country', true);
if(! empty($country_value)){
echo ' / ' . $country_value . ', ';
}
else{
echo ' / ';
}
$year_value = get_post_meta(get_the_ID(), 'release_year', true);
if(! empty($year_value)){
echo $year_value;
}
$catno_value = get_post_meta(get_the_ID(), 'catalog_no', true);
if(! empty($catno_value)){
echo ' / ' . $catno_value;
}
echo ')</p>';
endwhile;
echo '</ul>';
}
}
?>
Now I'm getting a list of all terms in the game
taxonomy and below each term I'm getting the title and permalink to each post associated with the term. This parts works fine.
The problem is the small paragraph I'd like returned below each post title/link. For some reason it returns the first value in the paragraph twice.
I.e. for the code bit that reads:
$number = intval(get_post_meta(get_the_ID(), 'number', true));
if($number > 1){
echo $number . 'x';
}
If $number = 1, it will display 1
(which is $number
) when it should display nothing. If $number = 2, it will display 22x
(which is $number . $number . 'x'
) when it should display 2x
.
If I comment out the code bit using $number entirely it will return 'Array' . $val . ', '
, because the next code bit returns the array $val.
If I comment out all the php within the paragraph I get the famed white screen. Presumably because it's trying to display some non-existant value.
Initially I thought it could be an error like the one explained here, which is why I have unset() placed in various locations, but that doesn't seem to affect anything displayed on the site.
I might be missing something obvious, but I really can't figure out why it's returning the firstcoming value. Can someone spot the error in the code?
echo $number . 'x';
? Or if you move it outside the `if($number > 1){``?echo $number . 'x';
then it displays$number
. If I move it outside the if statement, then it displays$number . $number . 'x'