For each wordpress category <option> (read!)

Discussion in 'PHP' started by Scorpiono, Dec 3, 2008.

  1. #1
    Hello there guys, I have this idea of displaying all the categories as a <select> and on click go to the category.

    I'm not familiar with wordpress tags, so I was wondering:

    <div id="categoryselect">
    <form>
    <select>
    {FOR EACH CATEGORY}<option value={CATEGORY}>{CATEGORY}</option>
    </select>
    </form>
    </div>

    No idea what to edit, and also if you know the javascript for automatically redirect upon click on any option, without any submit button. I believe I need value=URL.

    Thank you,
     
    Scorpiono, Dec 3, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    try this
    <?php wp_dropdown_categories('arguments'); ?> 
    PHP:
    example
    <?php wp_dropdown_categories(); ?>
    PHP:
    using javascript
    <li id="categories"><h2><?php _e('Posts by Category'); ?></h2>
    	<?php wp_dropdown_categories('show_option_none=Select category'); ?>
    
    <script type="text/javascript"><!--
        var dropdown = document.getElementById("cat");
        function onCatChange() {
    		if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
    			location.href = "<?php echo get_option('home');
    ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
    		}
        }
        dropdown.onchange = onCatChange;
    --></script>
    </li>
    PHP:
    another method
    <li id="categories">
    	<h2><?php _e('Posts by Category'); ?></h2>
    	<form action="<?php bloginfo('url'); ?>/" method="get">
    <?php
    	$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
    	$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    	echo $select;
    ?>
    	<noscript><input type="submit" value="View" /></noscript>
    	</form>
    </li>
    
    PHP:
    source : http://codex.wordpress.org/Template_Tags/wp_dropdown_categories
     
    misbah, Dec 3, 2008 IP
  3. Scorpiono

    Scorpiono Well-Known Member

    Messages:
    1,330
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Thanks a million mate
     
    Scorpiono, Dec 3, 2008 IP
  4. Scorpiono

    Scorpiono Well-Known Member

    Messages:
    1,330
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Works like a charm,though I have to ask if you know if <select> can have a background:url, I'm trying to stylize it a bit, not having much success.
     
    Scorpiono, Dec 3, 2008 IP
  5. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #5
    try this css
    select {
     background-color: #666699;
     color: #ffffff;
    }
    
    Code (markup):
     
    misbah, Dec 3, 2008 IP
  6. Scorpiono

    Scorpiono Well-Known Member

    Messages:
    1,330
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    120
    #6
    I think that should work, not sure about background images.
     
    Scorpiono, Dec 3, 2008 IP