WordPress.org

Ready to get started?Download WordPress

Forums

wp_dropdown_categories generating url ID number instead of slug (7 posts)

  1. Cixxy
    Member
    Posted 2 years ago #

    Hi all, this problem has me going nuts now.
    in http://staging.tahititravel.com.au/package/
    if you choose a category in the category filter box, the parent categories generate an ID number instead of the category slug in the URL. It's not SEO im worried about, it's that the WP_Query doesn't work if it doesnt generate a proper URL. So the box for sorting next to the category will not work.
    The categories within the hierarchy works fine.

    <form action="<?php bloginfo('url'); ?>/" method="get">
    					<?php
                        $select = wp_dropdown_categories('show_option_all=Filter by Category...&exclude=1,31,21&show_count=1&hide_empty=1&depth=3&hierarchical=false&orderby=name&echo=0&selected=6&taxonomy=category');
                        $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
                        echo $select;
                        ?>
                        <noscript><input type="submit" value="View" /></noscript>
                        </form>

    thanks

  2. Cixxy
    Member
    Posted 2 years ago #

    bump

  3. numer
    Member
    Posted 2 years ago #

    kk.

    i know that's quite late for an answer, but i'm quite fresh with wp. hope to help you or at least somebody else :)

    my idea:
    1) create child class for Walker_CategoryDropdown with little modified start_el function:

    class my_Walker_CategoryDropdown extends Walker_CategoryDropdown {
    
    	function start_el(&$output, $category, $depth, $args) {
    		$pad = str_repeat('&nbsp;', $depth * 3);
    
    		$cat_name = apply_filters('list_cats', $category->name, $category);
    		$output .= "\t<option class=\"level-$depth\" value=\"".$category->slug."\"";
    		if ( $category->term_id == $args['selected'] )
    			$output .= ' selected="selected"';
    		$output .= '>';
    		$output .= $pad.$cat_name;
    		if ( $args['show_count'] )
    			$output .= '&nbsp;&nbsp;('. $category->count .')';
    		if ( $args['show_last_update'] ) {
    			$format = 'Y-m-d';
    			$output .= '&nbsp;&nbsp;' . gmdate($format, $category->last_update_timestamp);
    		}
    		$output .= "</option>\n";
    	}
    }

    2) call wp_dropdown_categories with the new Walker object (not documented)

    wp_dropdown_categories(
              array(
    ...
                'walker' => new my_Walker_CategoryDropdown
              )
            );
  4. joshrodgers
    Member
    Posted 2 years ago #

    numer,

    This post helped me so much!! I tried everything from any forum I could find and nothing worked...except this! What I am trying to do is understand the code a little bit.

    I simplified the output a little by removing the counter and the update...the code now looks like: http://pastebin.com/VMWwcn1k.

    Everything else looks pretty self-explanatory (to me), but what does the $pad variable do? I see the repeat, the   and the $depth parameter, but I have no idea what it's doing.

    Just curious...thanks,
    Josh

  5. numer
    Member
    Posted 2 years ago #

    glad to help mate.

    you're right. pad = padding. it just moves the line one space to right to show deep of entity.

    look here to read sth about the function: http://php.net/manual/pl/function.str-repeat.php

    regards.

  6. joshrodgers
    Member
    Posted 2 years ago #

    numer,

    Thanks for the clarification :)

    Josh

  7. Tirumal
    Member
    Posted 2 years ago #

    Thanks numer I used your code to create a drop down to show all the post from particular taxonomy. I wrote a small tutorial for this. Read it on my blog.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.