while function and if statement returning incorrectly in WordPress

I am using the following code to list all the posts in my wordpress website and I am trying to call "selected" if the ID is the current cone selected but strangely it's calling the if statement 3 times rather than once even though all the ID's are different.

My Code:

function test($id) {
    // The Query
    query_posts( array ('posts_per_page' => -1 ) );
    // The Loop
     while ( have_posts() ) : the_post(); 
     if ($id == get_the_ID()) { $selected = " selected"; }
     ?>
    <option value="<?php echo get_the_ID(); ?>"<?php echo $selected; ?>><?php echo get_the_title() ?></option><?php echo "\n"; ?>
    <?php endwhile;
    // Reset Query
    wp_reset_query();
    }

My Output:

<select name="w_url">
<option value="493">TEST 1</option>
<option value="390">TEST 2</option>
<option value="388" selected>Test 3</option>
<option value="386" selected>Test 4</option>
<option value="384" selected>Test 5</option>
</select>

I did select the third option and so it is showing that one correctly but carries on to the 4th and 5th. I'm sure its something simple but I can't see it

Solutions

After your variable $selected gets the value "selected", it doesn't change value in futher iterations. You should use something like

if ($id == get_the_ID()) { 
    $selected = " selected"; 
} else { 
    $selected = ""; 
}
Tags: PHP / Wordpress

Similar questions

Else statement not triggering after a failed if statement
I am working on a client site and the statement I wrote 6 months ago seems to have stopped working after the WordPress update to 3.8 I can't see or figure out why. There are no errors appearing it just returns empty divs with post Id's in the class field. If the the if statement is false then it needs to echo out different data. But sadly it isn't ...
PHP Structuring an if statement inside an if else statement
I have created an if statement inside the "else" of an if/else statement.It works.However the code below the function does not run.When i remove the function the code below the function runs.Could you please help me out. This code causes the problem: The php:
if statement not working, outputs else statement instead
I'm trying to output the if statement, but instead the else statement shows. I have created a page using Wordpress and assigned this template to the page. I'm trying to show product image and content if number of products is > 0, which it is. But instead it shows the else statement. Can someone please help?
IF statement is ignoring 2nd nested IF statement
I wrote the following piece of code and the 2nd nested IF statement is being ignored. If I switch the order of them the same thing happens. I've tried an elseif statement and that doesn't seem to work either. Any ideas why? I'm guessing it's a pretty noob question, so please skool me :)
Fatal error: Namespace declaration statement has to be the very first statement in the script in /home/central1/public_html/wp-content
How to remove:- Fatal error: Namespace declaration statement has to be the very first statement in the script in /home/central1/public_html/wp-content/plugins/wcp-openweather/classes/Settings.class.php on line 2
Unsetting session in if statement. If statement ignored
I have a wierd problem with $_SESSION in woprdpress namely I would like to clear session variables on all pages except single pages of custom post type - product. I use the condition: The problem is the session is cleared on all pages including the product single pages. The if statement is simply ignored. I tried placing it in template_redirect hoo...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.