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 "
"; ?>
<?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