John Davidson

php - Wordpress only shows practice categories with practices attached to it

0 comments
Message:


I have a problem with my function is use for dropdown menus in wordpress.
It only shows practice categories that have practices attached to it. If i remove the last forloop, it does show all the practice categories and vice-versa. Im a big php noob.
Thanks.


function lr_get_practice_select_data() {
$practiceCategories = get_terms(array(
'taxonomy' => 'practice_category',
'orderby' => 'name'
));
$out = array();
foreach ($practiceCategories as $category) {
$out[] = array(
'value' => 'category_' .$category->term_id,
'class' => 'category',
'name' => $category->name
);
$practicesInCategory = lr_get_practices_by_category($category->term_id);
foreach ($practicesInCategory as $practice) {
$out[] = array(
'value' => $practice->ID,
'class' => 'practice',
'name' => $practice->post_title
);
}
}
return $out;
}



My Answer:
> 'practice_category',
'hide_empty' => false
));

$options = '';

foreach ($practiceCategories as $category) {
$practices = get_posts(array(
'post_type' => 'practice',
'tax_query' => array(
array(
'taxonomy' => 'practice_category',
'field' => 'term_id',
'terms' => $category->term_id
)
)
));

if (!empty($practices)) {
$options .= '';
foreach ($practices as $practice) {
$options .= '';
}
$options .= '
';
}
}

return $options;
}

Rate this post

4 of 5 based on 1959 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us