How to display the child category of a specific parent category…

Home Forums Calendar Products Events Calendar PRO How to display the child category of a specific parent category…

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #931984
    Gabriel
    Participant

    Hi,

    i want to show child category of a specific parent category. In the normal Template (single.php) that works:

    <?php
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(7, $childcat)) {
    echo $childcat->cat_name;
    }}
    ?>	

    How does ist work in singel-event.php?

    I try this, but it dont work.

    <?php
    foreach((tribe_get_event_categories()) as $childcat) {
    if (cat_is_ancestor_of(7, $childcat)) {
    echo $childcat->cat_name;
    }}
    ?>	

    Thanks for your help!

    #932429
    Brook
    Participant

    Howdy Gabriel,

    Good question. You actually can use the builtin WP function to do it in both cases: get_categories(). As you can see you can pass it the parent category ID via ‘child_of ‘, so it will only retrieve the children of that category. No need to bother with an if inside of a foreach. So just run get_categories() and set child_of to the parent ID. If you want to do this for Events categories then you can set the taxonomy to ‘tribe_events’. And there you have it, the complete list!

    Does that make sense? Did that work? Please let me know.

    Cheers!

    – Brook

    #932641
    Gabriel
    Participant

    If you want to do this for Events categories then you can set the taxonomy to ‘tribe_events’.

    What do you exactly mean with it? I try this, but it dont work 🙁

    <?php
    $args = array(
      'taxonomy' => 'tribe_events'
      );
    $categories = get_categories($args);
    foreach((get_the_category()) as $childcat) {
    if (cat_is_ancestor_of(7, $childcat)) {
    echo $childcat->cat_name;
    }}
    ?>

    I am sorry, I think my WordPress Codex know-how is not enough here.

    #933928
    Brook
    Participant

    Howdy Gabriel,

    What do you exactly mean with it? I try this, but it dont work

    You got it right, you set the taxonomy. But, then you didn’t call that variable in the rest of your code. Nor did you set child_of. For what it’s worth, when you are writing in a programming language usually you need to either be a programmer of hire one. The support we offer here is meant to be a general overview as outlined in our scope of support. We do not provide specific code to your site. It is up to you to turn the overview into working code.

    That said sometimes I can sneak a little site specific code past my manager. I have not tested this and it might take a little tweaking, but this is a really good start:

    [php]
    $categories = get_categories(array(
    ‘taxonomy’ =&gt; ‘tribe_events’,
    ‘child_of’ =&gt; 7,
    ));
    foreach($categories as $childcat) {
    echo $childcat-&gt;cat_name;
    }
    [/php]

    I also just realized that my link above doesn’t work. This might help you to further your knowledge of that function: http://codex.wordpress.org/Function_Reference/get_categories

    Hopefully that helps. Cheers!

    – Brook

    #949831
    Brook
    Participant

    Since this topic has gone for a spell without an update it is getting archived. If you need anything else though please feel free to open a new topic, we would love to help. Cheers!

    – Brook

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘How to display the child category of a specific parent category…’ is closed to new replies.