I don't want to write out something like this from 1 to 1000:

        <select name="number_pick" id="number_pick" >    
        <option value="1" <?php selected( '1', get_the_author_meta( 'number', $user->ID ) ); ?>>1</option>
        <option value="2" <?php selected( '2', get_the_author_meta( 'number', $user->ID ) ); ?>>2</option>
        <option value="3" <?php selected( '3', get_the_author_meta( 'number', $user->ID ) ); ?>>3</option>
        </select>   

So instead, I am trying to use a foreach statement to get from 1 to 1000. I think I am close, but I have something wrong and I don't know what it is.

            <select name="number_pick" id="number_pick" >
            <?php foreach (range(0,1000) as $number)
            echo "<option value='$number' selected( '$number', get_the_author_meta( 'number_pick', $user->ID ) ) >$number</option>"; ?>
            </select>

Can anyone tell me what I am missing in the lines above?

Here is the complete code:

        <?php
            add_action( 'show_user_profile', 'show_extra_profile_fields' );
            add_action( 'edit_user_profile', 'show_extra_profile_fields' );

            function show_extra_profile_fields( $user ) { ?>
                <h3>Extra profile information</h3>
                <table class="form-table">
                    <tr>
                        <th><label for="number_pick">Pick A Number</label></th>
                        <td>
                            <select name="number_pick" id="number_pick" >
                                <?php foreach (range(0,1000) as $number)
         echo "<option value='$number' selected( '$number', get_the_author_meta( 'number_pick', $user->ID ) ) >$number</option>"; ?>
                            </select>
                        </td>
                    </tr>
                </table>
            <?php }

            add_action( 'personal_options_update', 'save_extra_profile_fields' );
            add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

            function save_extra_profile_fields( $user_id ) {
                if ( !current_user_can( 'edit_user', $user_id ) )
                    return false;
                update_usermeta( $user_id, 'number_pick', $_POST['gender'] );
            }
        ?>

没有正确的解决方案

许可以下: CC-BY-SA归因
scroll top