• firehold

    (@firehold)


    Hello,
    at the moment i am using the code below wo receive a list of all of my categories.
    In my case i want to add a checkbox before every category except the parent categories, these should be blank.

    Now i have an array which contains ids of categories which should be able to display, all other categories shouldn’t be displayed/in the output.
    f.e. $allowed = array(15,24,44,90);

    I read about the inlude/exclude arguements but wasnt able to find clear description how they work. Are they a solution for my needs? Or can i work with the get_categories filter reference?

    Thank you very much in advance!

    <ul style="list-style-type: none;">
        <?php // get parent categories
        $parent_cats = get_categories( array(
            'orderby' => 'name',
            'parent'  => 0,
            'hide_empty'=> false
        ));
        
        foreach ( $parent_cats as $pcat ) {
    
            // get child categories
            $child_cats = get_categories( array(
            'orderby' => 'name',
            'parent'  => $pcat->term_id,
            'hide_empty'=> false
            ) ); 
            
            // if childs, output parent without and childs with checkbox
            if ($child_cats) { ?>
                <li>
                    <label>
                        <?php echo $pcat->name; ?>
                    </label>
                
                    <ul style="list-style-type: none;">
            <?php   foreach ( $child_cats as $ccat ) {
                        if ($ccat) { ?>
                            <li>
                                <label>
                                    <input type="checkbox" class="choose-category" <?php if($this->ztgm_abo_cats[0] != '' AND in_array($ccat->term_id,$this->ztgm_abo_cats[0])) { echo 'checked'; } ?> name="categories[]" value="<?php echo $ccat->term_id; ?>"> <?php echo $ccat->name; ?> ( <?php echo $ccat->term_id;?> )
                                </label>
                            </li>
            <?php       }  
                    } ?>
                    </ul>
                </li>
      <?php }
            // output parent with checkbox
            else { ?>            
                <li>
                    <label>
                        <input type="checkbox" class="choose-category" <?php if($this->ztgm_abo_cats[0] != '' AND in_array($pcat->term_id,$this->ztgm_abo_cats[0])) { echo 'checked'; } ?> name="categories[]" value="<?php echo $pcat->term_id; ?>"> <?php echo $pcat->name; ?> ( <?php echo $pcat->term_id;?> )
                    </label>
                </li>
      <?php }
        } ?>
    </ul>
    • This topic was modified 7 years ago by firehold.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You are sooo close! 🙂
    Include your $allowed array as an “include” argument in the array passed to get_categories(). Like so:

    <?php // get parent categories
        $allowed = array(15,24,44,90);
        $parent_cats = get_categories( array(
            'orderby' => 'name',
            'parent'  => 0,
            'hide_empty'=> false,
            'include' => $allowed,
        ));

    The “exclude” argument works exactly opposite, pass an array of term IDs you don’t want returned. Don’t use both at the same time or WP gets confused.

    One little PHP coding tip: in PHP, it’s permissible in array declarations to have a trailing comma even though there is not a subsequent element. By always including a trailing comma after every element, it’s easier to edit code and manage it later on down the line. It avoids stupid mistakes where one forgets to add a comma to the list during edits. This is only for arrays, don’t include trailing commas in function parameters. The parser does not like them there.

    ETA: I’m assuming these IDs were all top level category terms. Otherwise the “parent” argument will conflict with any child term IDs.

    • This reply was modified 7 years ago by bcworkz.
Viewing 1 replies (of 1 total)

The topic ‘get_categories include/exclude arguments’ is closed to new replies.