SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Location
    Washington D.C
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wordpress category dropdown

    In worpress has anyone been anyone been able to change the category checkboxes to a category dropdown box when a user is posting? I was looking through admin-functions.php and found the following code. I have posted what they have and my attempts to get the dropdown to get wordpress to capture the "selected" category in the dropdown. Right now the only thing that is happing is it is just capture the default category regardless of what is selected. If there is a plugin that can do this well that would be helpful too.

    Thanks, Ryan


    Their function:

    Code:
    function write_nested_categories( $categories ) {
        foreach ( $categories as $category ) {
            echo '<li id="category-', $category['cat_ID'], '"><label for="in-category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="in-category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : "" ), '/> ', wp_specialchars( $category['cat_name'] ), "</label></li>";
    
            if ( $category['children'] ) {
                echo "<ul>\n";
                write_nested_categories( $category['children'] );
                echo "</ul>\n";
            }
        }
    }

    My Rewrite:

    Code:
    function write_nested_categories( $categories ) {
        echo "<select>\n";
        foreach ( $categories as $category ) {
            echo '<option id="category-', $category['cat_ID'], '" value="', $category['cat_ID'],  '" name="post_category[]" >', $category['cat_name'], '</option>     ';
    
            if ( $category['children'] ) {
                echo "<ul>\n";
                write_nested_categories( $category['children'] );
                echo "</ul>\n";
            }
        }
        echo "</select>\n";
    }

  2. #2
    Is Still Alive silver trophybronze trophy RetroNetro's Avatar
    Join Date
    Jul 2003
    Location
    Texas
    Posts
    6,883
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am not sure how to do what you want, but it seems to me that the checkboxes are the only way to go.

    What will you do if you need to add a post to more than one category and all you have is a drop down list?

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Location
    Washington D.C
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That it is not a requirement. I just need to post one category to one post.

  4. #4
    code ninja hamstu's Avatar
    Join Date
    Nov 2002
    Location
    Montréal, Canada
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with Johnny, doing this will make it impossible to post to more than one category. I would suggest adding multiple="multiple" to your select.
    Edit: Ok, I see that's not an issue for you.

    Also, in your code, there's a recursive call for categories with children, and AFAIK, you can't have a <select> within a <select> so you should watch out for that.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Location
    Washington D.C
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post an example on how to do that?

  6. #6
    Non-Member QiSoftware's Avatar
    Join Date
    Oct 2004
    Location
    U.S.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try something like this--

    PHP Code:
     echo '<option value='."$category['cat_ID']".' >'"$category['cat_name']" .'</option> '
    Q...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •