Counter in loop in foreach

EDIT Ok so after better understanding your issue, I would not use a counter but a true/false flag that we switch when it’s been used. Try this code. $cats = array(‘batiments-delevage’, ‘logements-bovin’, ‘stockages-deffluents’, ‘soins-et-hygiene’); $exclude_posts = array(); $active = true; foreach( $cats as $cat ) : // build query argument $query_args = array( ‘category_name’ => … Read more

Multiple categories with common subcategories

What you could do is use a mixture of Categories and Tags if you prefer to prevent duplication of categories and confusion for the post authors. Use the Years as categories and Blog/Announcement as tag. So, as example: If they post a Blog for “Year 2”, they post it in the category “Year 2” and … Read more

Category name field strips HTML tags. How can I reverse this?

As of WordPress 4.6, it’s not possible to save html in the category name field. Technical Reason When categories (or any taxonomy for that matter) are saved, wp_update_term() runs and passes the values through sanitize_term() which strips out any html. $args = sanitize_term($args, $taxonomy, ‘db’); Alternative Solution I’m not sure how you’re displaying category names … Read more

Access current post parent and sub category

Try it like this: $categories = get_the_category(); echo ‘<ul>’; echo ‘<li> Parent Category: ‘; foreach( $categories as $category ){ if($category->parent != 0){ $parent_category = get_term( $category->parent ); echo ‘<a href=”‘ . esc_url( get_category_link($parent_category->term_id)) . ‘”>’ . esc_html($parent_category->name) . ‘ </a>’; break; } } echo ‘</li>’; echo ‘<li>Subcategory: ‘; foreach( $categories as $category ){ if($category->parent != … Read more

WordPress query via checkbox form

The scope of your question is pretty broad, but I think I can help. Set the name to as the same name for the ones you want grouped using array syntax ([]). Set the checkboxes’ values to the category ID. // … foreach ( $sub_categories as $sub_category ){ echo ‘<label><input type=”checkbox” id=”type-‘. $sub_category->name . ‘” … Read more

Category shows only top level child categories

‘depth’ is not a parameter for the function get_categories(); to get only direct child categories, try working with ‘parent’ instead of ‘child_of’ https://developer.wordpress.org/reference/functions/get_categories/ https://developer.wordpress.org/reference/functions/get_terms/