• salocined

    (@salocined)


    Hi,

    I have been scratching my head for a while and can’t seem to find the right information, not sure I am doing this right.

    I have a the following categories
    – Country A
    –City A
    —Hotels in city A
    —-Hotel type 1
    —-Hotel type 2
    etc….

    City is a Post.
    When in City, I like to display Hotel Type 1, Hotel type 2 as a listing

    To get the right category ID when in city I do

    $currentcategory = get_the_category();
     $currentcat = $currentcategory[0]->cat_name;
     $currentcategory_id = get_cat_ID($currentcat);

    $currentcategory_id allows me to get the City category ID and then I try to get the child posts of the categories below (hotels)

    Then I do a query_posts to list the Hotels for this city
    query_posts('child_of='.$currentcategory_id);

    Am I complicating it too much?

    Help really appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter salocined

    (@salocined)

    bump?

    Michael

    (@alchymyth)

    if it is working, what is the question?

    sounds like you already know how to do it.

    Thread Starter salocined

    (@salocined)

    Hi Alchymyth,

    No it’s not working.
    I am not getting the correct child category ID.
    $currentcategory_id is giving me the CITY A category.
    But what I need is the category ID of Hotels in city A (1st child of current category).
    If somebody can help me getting this ID, then am ok to list the rest

    Thx

    S

    Michael

    (@alchymyth)

    so, you basically wnat to show the posts of the child-child category of ‘city’ ?

    do you need to go through all child categories of ‘city’ i.e. the ‘hotels in city a’ level ?

    the categories of the level ‘hotels in a city a’ should be:

    $childs_of_current_cat = get_categories('parent=' . $currentcategory_id);

    if there is only one category, then you could use:
    $child_of_current_cat = $childs_of_current_cat[0]->term_id;

    if there are more than one, and you need to go through all of them, you will need to use a ‘foreach’ loop.

    https://codex.www.ads-software.com/Function_Reference/get_categories

    Thread Starter salocined

    (@salocined)

    $childs_of_current_cat = get_categories('parent=' . $currentcategory_id); is returning ALL parents categories, not the childs?

    Michael

    (@alchymyth)

    weird, according to the instruction, it shoud return only direct child categories of the category with the id in $currentcategory_id

    what is the output if you do a var_dump($childs_of_current_cat) ?

    i would expect it to show ‘—Hotels in city A’ when you are in the post of level ‘–City A’

    Thread Starter salocined

    (@salocined)

    I am getting array(0) { }
    This is why it’s displaying all categories…

    $currentcategory_id is definitely correct and return the correct Category ID for the post.
    I just need to get the first child Category ID (Hotels in City A), this way I can list my posts.

    Weird….

    Thx for you help, appreciated

    Thread Starter salocined

    (@salocined)

    Ok, may be a different way
    If using
    wp_list_categories('orderby=id&use_desc_for_title=1&depth=1&hierarchical=1&hide_empty=0&child_of='.$currentcategory_id);

    I am getting the correct Category on the page but displayed as

    * Categories
    Hotels in in CITY A

    How can I extract the ID from Hotels in city A and use it in my query posts?

    Thread Starter salocined

    (@salocined)

    Ok I’ve got it working, might be useful for others

    //get current cat ID
    $currentcategory = get_the_category();
    $currentcat = $currentcategory[0]->cat_name;
    $currentcategory_id = get_cat_ID($currentcat);
    
    //Get Child of current cat
    $currentcategory = get_categories('orderby=id&use_desc_for_title=1&depth=1&hierarchical=1&hide_empty=0&parent='.$currentcategory_id);
    foreach($currentcategory as $category) {
    $catid = $currentcategory[0]->term_id;
    }

    $catid will be the first child category ID

    Hey @salocined,

    Where does that code reside in your page? Before or after loop?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display Post of Child Category only’ is closed to new replies.