1

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?

2
  • What happens if you comment out echo $number . 'x';? Or if you move it outside the `if($number > 1){``?
    – fischi
    Commented Feb 9, 2016 at 16:33
  • If I comment out echo $number . 'x'; then it displays $number. If I move it outside the if statement, then it displays $number . $number . 'x'
    – rpbtz
    Commented Feb 9, 2016 at 17:17

1 Answer 1

0

I managed to fix it! As I suspected it had nothing to do with the code relating to $number, but rather the echo call before it.

My initial code looked like this (I'm skipping part of the loop content to keep it simple):

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';
    }

echo ')</p>';

If $number = 1 it would display $number and if $number > 1 ) it would display $number . $number . 'x'

Looking through the code here some time later I noticed something off. In my first echo call I finish with an . so the following code could be added in my paragraph. But when finishing up the paragraph I added a new echo call instead of following up the loop code with . '</p>'.

What I did to fix it was to end my initial echo call instead of continuing it, so to fix it I only had to change one character and remove a space in the first line, so it looks like this:

echo '<p style="color:grey;font-size:11px;margin:0;">(';

Note the ending of that piece of code compared to how it looked before I fixed it. I simply replaced . with ;.

I don't know enough php to explain this behavior, although I assume it's got something to do with not ending/closing the echo properly. If someone else can explain more accurately, please feel free to add a comment and I'll update this answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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