0

I researched but did not find right answer to my question

I have custom post type for states, cities and countries and would like to set the default value in the country list for "Argentina", but do not know how to solve it.

see my code:

        <select name="pais" class="pais-select">

                    <option value="">País</option>
                    <?php
                        $pais = diretorio_request('pais');
                        foreach(diretorio_countries() as $country) {
                    ?>
                    <option value="<?php echo $country; ?>"<?php echo ($pais == $country) ? ' selected="1"' : ''; ?>><?php echo $country; ?></option>
                    <?php
                        }
                    ?>
                </select> 

I tried something like "selected = selected" but does not mark Argentina scored Zimbábue (rsrs)

2 Answers 2

0

should be selected="selected" NOT "selected=selected"

EDIT:

Ok I see - it's because you are using a foreach arguement to say that it's selected. Meaning that it prints the HTML showing that everything should be selected.

Try something like this:

<select name="pais" class="pais-select">
<option value="" disabled selected>País</option>
<option value="Argentina">Argentina</option>
<?php
$pais = diretorio_request('pais');
foreach(diretorio_countries() as $country) {
?>
<option value="<?php echo $country; ?>"><?php echo $country; ?></option>
<?php
}
?>
</select> 

or I suppose you could do something like

<option value="<?php echo $country; ?>" <?php if ($country == Argentina) { echo 'selected="selected" ' }  ?>"><?php echo $country; ?></option>

Depending on how the values are stored!

2
  • I tried but the result display is zimbaube
    – Kadix
    Commented Jul 10, 2014 at 13:11
  • @Kadix Ah ok - yes sorry - a quick reply as I didn't read the code properly. I have updated the answer for you to try
    – Bysander
    Commented Jul 10, 2014 at 15:29
1

I tried your suggestion and only needed to make a small adjustment... removing the "{" and worked

the code looked like this

<option value="<?php echo $country; ?>" <?php if ($country == Brasil) echo 'selected="selected" ';?>><?php echo $country; ?></option>

thank you!

1
  • Not a problem - apologies I wrote it directly into WP stack exchange so didn't have any prompts or console to check!
    – Bysander
    Commented Jul 14, 2014 at 12:01

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.