Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End how to show value of select dropdown?

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #162644
    cybershot
    Participant

    I haven’t used select boxes much. I am using one in a new theme I am making. I have the dropdown and values working. The problem I have now is that the dropdown allows you to select a category that you want to display on a page in WordPress. When I choose my category and save the page, the first category in the list displays. I want the dropdown list to show the selected category.

    #162646
    TheDoc
    Member

    Are you ending up on a category page? If so, that’s be quite easy:

    <?php if( is_cat('Fun') { echo 'selected'; } ?>
    

    If you’re actually using POST on a form you could do something like this:

    <?php if( $form["category"] == 'cat-name' ) { echo 'selected'; } ?>
    
    #162647
    TheDoc
    Member

    Of course, you’re probably not writing these out manually, so you could do something like this:

    $categories=get_categories($cat_args);
    
    foreach($categories as $category) {
        echo "<option name.... yadda yadda
    }
    
    #162649
    cybershot
    Participant

    I am using the select in a page builder in the admin section of WordPress. It is being used to get the category to generate a shortcode that will call a custom loop to query that category

            <option value="" default selected>Select your category</option>
            <option value="discography">Discography Page</option>
                <option value="contact">Contact Page</option>
                <option value="images">Images Page</option>
                <option value="news">News Page</option>
                <option value="shows">Shows Page</option>
                <option value="band">Band Page</option>
                <option value="videos">Videos Page</option>
    

    The select box is inside a WordPress widget code. I was looking for something like the textbox has. Some way of showing the selected value.

    #162651
    cybershot
    Participant

    I just figured it out.

    <option value="contact" <?php if($instance['cats'] == 'contact'){ echo 'selected'; } ?>>Contact Page</option>

    #162705
    TheDoc
    Member

    Nice one! Hopefully what I provided helped!

    #162706
    cybershot
    Participant

    Yes, it helped. Thanks

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.