• davidknight42

    (@davidknight42)


    Hi All
    Am building a widget that allows the user to choose one post from the widget admin panel that will be displayed on the front page.

    I have a dropdown list that shows all posts, it displays, it saves, it works…apart from adding “selected’ to the actual selected post. So the problem is if you change another option in the widget, it saves back to the first option on the list.

    Here is the code I have right now.

    <select name="<?php echo $this->get_field_name('title'); ?>" id="<?php echo $this->get_field_id('title'); ?>" class="widefat">
    <?php
    global $post;
    $args = array( 'numberposts' => -1, 'post_type' => array ( 'post', 'page','review'));
    $posts = get_posts($args);
    foreach( $posts as $post ) : setup_postdata($post);
    ?>
    <option value="<? echo $post->post_title; ?>"<?php selected( $instance['title'], '<?php the_title(); ?>' ); ?>><?php the_title(); ?></option>
    <?php endforeach; ?>
    </select>

    Can anyone help me add selected to the chosen option value?

    Thanks in advance

The topic ‘Adding selected to post in dropdown list problem’ is closed to new replies.