WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_categories() not functioning as expected (4 posts)

  1. relish1227
    Member
    Posted 4 years ago #

    I am using the dropdown box example found here: http://codex.wordpress.org/Function_Reference/get_categories

    My call goes:
    $categories = get_categories('child_of=13&orderby=name');

    However, I am seeing two issues:

    1. It is not displaying by name.
    2. It is displaying multiples of each category. Here is the output:

    Alterations – Clothes & Drapes (1)
    Alterations – Clothes & Drapes (1)
    Automotive – Repair, Service & Tires (3)
    Alterations – Clothes & Drapes (1)
    Automotive – Repair, Service & Tires (3)
    Dry Cleaning (1)
    Alterations – Clothes & Drapes (1)
    Automotive – Repair, Service & Tires (3)
    Dry Cleaning (1)
    Health, Fitness & Wellness (4)
    Alterations – Clothes & Drapes (1)
    Automotive – Repair, Service & Tires (3)
    Dry Cleaning (1)
    Health, Fitness & Wellness (4)
    Tailors (1)

    I can't figure out why this isn't working... One thing that I do have that is different here is that these are Flutter posts.

    Any ideas?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Can i see the code that follows this line.

    $categories = get_categories('child_of=13&orderby=name');

    ..and deals with the output..

  3. relish1227
    Member
    Posted 4 years ago #

    Duh. I figured it out.

    I was adding in a conditional in the loop there --

    if(trim($catID) == $cat->cat_ID)
    {
    	$option .= '<option value="/our-community/business-directory/?cat='.$cat->cat_ID.'" selected="true">';
    }
    else
    {
    	$option .= '<option value="/our-community/business-directory/?cat='.$cat->cat_ID.'">';
    }

    However, I needed either "$option = '';" above this or just to set each item to "$option =" instead of "$option .=".

    So it was adding previous items to each loop. What a different . will make!

    C

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    As long as you've got your problem fixed, good stuff... ;)

    Small note though, for options that are selected, i believe the correct usage is.

    selected="selected"

    Although technically speaking i believe either will actually work.

Topic Closed

This topic has been closed to new replies.

About this Topic