• Resolved visualtricks

    (@visualtricks)


    ..well, expected by me at least ??

    hi everyone, i’ll jump to the point: I’ve a page that issues a query and for every post i’m fecthing post terms with wp_get_post_terms(); Inside every post, i’m cycling through post terms and if one of them has a specific ID, i’m setting a variable to 1. At the end of the post, if the varable is 1, i’m flagging the post as “reserved” (or doing stuff anyway).
    Its almost working, except for the last part, because as soon as i get out the term cycling, my variable is always 1 no matter what.

    This is an extract of the code i’m using:

    while ( $query->have_posts() ) : $query->the_post();
    
    	$res = 0;
    	$postID = $post->ID;
    	terms = wp_get_post_terms($postID,'tcp_product_category');
    
    	foreach ($terms as $term_n) {
    	    $termid = $term_n->term_id;
    	    if ($termid == 8) {$res = 1; echo $res; } else {$res = 0; echo $res; }
    	} //end foreach - this part is working good
    
    	if ($res = 1) {
               echo "Reserved: ".$res;
            } else {
               echo "Not Reserved: ".$res;
            }  // this isn't working, my $res is always 1
    
    endwhile;

    In a nutshell, what I’m trying to do is check if a post contains a specific term and flag it in someway to treat it differently from other posts. Can anyne point me in the right direction ?

    Thanks
    M.

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Setting a variable in a loop not working as expected..’ is closed to new replies.