• Hi

    I have created a custom widget to display a custom category. The widget for my custom category works fine and in the same way that the original widget for categories.
    The problem that I have is that when I select to display the custom categories in a drop down list, the links to the different items don’t work. However when they are display on a list it works.

    When the taxonomy is displayed as a list it generates the url as something like https://myurl/?video_category=slug where video_category is the name of my custom taxonomy and slug is the slug of the option selected. As I said in this case it works and it returns the post under that category.
    When the taxonomy is displayed as a hierarchy it generates the url as
    https://myurl/?video_category=id where id is a number which must be the id of the term of the taxonomy. In this case it doesn’t work and it says no posts found (however there are posts, i select the same value that works when displaying in a list). I have also checked in the database that there is a term with that id.

    Has anyone any idea of what can i have wrong?

    In the code of my widget i have the following location.href = "<?php echo home_url(); ?>/?video_category=" + dropdown.options[ dropdown.selectedIndex ].value to generate the url. This is copy from the original category widget. I can see that it is here where the url is generated, but i don’t know:
    1) why I can’t find my post when using ?video_category=id if for a normal category the same works. is it possible to have anything wrong on the definition of my category (custom), if so, what?
    2)is it possible to change that value returns the slug instead of the id?
    Thanks in advance
    Teresa

Viewing 6 replies - 1 through 6 (of 6 total)
  • Make sure that your code for generating the select box uses the slug in the value=”” of the option.

    Thread Starter tnoguero

    (@tnoguero)

    Hi, sorry I don’t understand. In my widget I have
    $cat_args = array(
    ‘orderby’ => ‘hierarchical’ => $h,
    ‘taxonomy’ => ‘video_category’,
    );
    if ( $d ) // when displaying as dropdown
    {
    $dropdown_id = ( first_dropdown ) ? ‘video_category’ : “{$this->id_base}-dropdown-{$this->number}”;
    $first_dropdown = false;
    echo ‘<label class=”screen-reader-text” for=”‘ . esc_attr( $dropdown_id ) . ‘”>’ . ‘</label>’;

    $cat_args[‘show_option_none’] = __( ‘Select a Category’, ‘acquire-widgets’ );
    $cat_args[‘id’] = $dropdown_id;

    /** * Filter the arguments for the Categories widget drop-down.
    *
    * @since 2.8.0
    *
    * @see wp_dropdown_categories()
    *
    * @param array $cat_args An array of Categories widget drop-down arguments.
    */

    wp_dropdown_categories( apply_filters( ‘widget_categories_dropdown_args’, $cat_args ) );
    ?>

    <script type=’text/javascript’>
    /* <![CDATA[ */
    (function() {
    var dropdown = document.getElementById( “<?php echo esc_js( $dropdown_id ); ?>” );
    function onCatChange() {
    if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {

    location.href = “<?php echo home_url(); ?>/?video_category=” + dropdown.options[ dropdown.selectedIndex ].value;
    }
    }
    dropdown.onchange = onCatChange;
    })();
    /* ]]> */
    </script>

    On the line after:

    $cat_args['id'] = $dropdown_id;

    Add:

    $cat_args['value_field'] = 'slug';

    Thread Starter tnoguero

    (@tnoguero)

    Hi
    I had tried that before (and now again ?? ) but when i do that it doesn’t do anything when i select a value in the drop down list. It doesn’t generate any query/url.

    do you know why?

    Thread Starter tnoguero

    (@tnoguero)

    The url is https://13.75.40.34/ if you want to see what i mean, i have applied the change you suggested.

    Also, do you know why it doesn’t work when it is https://myurl/?video_category = 35 but it does when it it https://myurl/?cat=91?

    Thanks for your help.

    Thread Starter tnoguero

    (@tnoguero)

    Hi, does anyone have any other idea?

    $cat_args[‘value_field’] = ‘slug’; doesn’t work.

    Or if anyone can point why I can’t access when using the id to my custom taxonomy.

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Link to custom category not working when using the id’ is closed to new replies.