Forums

Get Parent Categories as Checkbox Inputs (1 post)

  1. dhechler
    Member
    Posted 7 months ago #

    Heres a simple way to get parent categories only into a list of checkbox values with the category ID as the value. Just use <?php cat_list(); ?> where you want the list to appear.

    function cat_list(){
    		$categoryargs = array(
    		'hide_empty' => false,
    		'walker' => 'object',
    		'depth' => 1
    		);
    		$categories=get_categories($categoryargs);
    		foreach($categories as $category) {
    			if ($category->category_parent == 0){
    				echo "<input type='checkbox' name='mychecky' value='$category->term_id' />";    echo $category->cat_name;echo '<br>';
    			}
    		}
    	}

Reply

You must log in to post.

About this Topic

  • RSS feed for this topic
  • Started 7 months ago by dhechler
  • This topic is not a support question
  • WordPress version: 3.2.1