How to determine if a category is a child of another category

Facebook
Twitter
LinkedIn

This WordPress code snippet is a custom function that hides a specific element on pages that match certain conditions related to a custom taxonomy. Let’s break it down step by step to understand what each part does and how it all works together.

<?php

add_action( 'template_redirect', function () {
	$term_id = get_queried_object_id();
	if ( is_tax( 'bl_category' ) && term_is_ancestor_of( 2981, $term_id, 'bl_category' ) ) {
		$styles = '<style>.tdb-filter-bl_tag {display: none;}</style>';
		add_filter( 'the_content', function ( $content ) use ($styles) {
			return $styles.$content;
		});
		}
} );

1. Understanding add_action():

  • add_action(‘template_redirect’, function () {…}): This line hooks a custom function into the template_redirect action in WordPress. The template_redirect action is triggered before the template file is loaded but after WordPress determines which template to use. It’s often used to perform actions based on the current page or to modify the page content before it’s displayed.
  • Anonymous Function: The function defined within add_action is an anonymous function (closure) that will run when template_redirect is triggered.

2. Getting the Term ID:

  • $term_id = get_queried_object_id();: This line retrieves the ID of the currently queried object, which in this context is expected to be a taxonomy term. The get_queried_object_id() function returns the ID of the object that WordPress is currently displaying, such as a post, page, or taxonomy term.

3. Checking the Conditions:

  • if (is_tax(‘bl_category’) && term_is_ancestor_of(2981, $term_id, ‘bl_category’)) { … }:
  • is_tax(‘bl_category’): This checks if the current page is a taxonomy archive for the bl_category taxonomy. The is_tax() function checks if the current page is a taxonomy archive page for the specified taxonomy.
  • term_is_ancestor_of(2981, $term_id, ‘bl_category’): This checks if the term with ID 2981 is an ancestor of the current term ($term_id) in the bl_category taxonomy. The term_is_ancestor_of() function is used to determine if one term is an ancestor of another within a hierarchical taxonomy. An ancestor term is a parent or higher-level term in the taxonomy hierarchy.
  • If both conditions are true, it means that the current page is an archive page for a bl_category taxonomy term that is a descendant of the term with ID 2981.

4. Adding Inline Styles to Hide an Element:

  • $styles = ‘<style>.tdb-filter-bl_tag {display: none;}</style>’;: This line creates a string containing a <style> block with CSS that hides elements with the class .tdb-filter-bl_tag. The display: none; CSS rule makes the element invisible on the page.

5. Appending the Styles to the Content:

  • add_filter(‘the_content’, function ($content) use ($styles) {…});: This line adds a filter to the the_content hook. The the_content filter allows you to modify the content of a post or page before it is displayed.
  • Anonymous Function: The anonymous function takes the existing content ($content) and appends the $styles string (which contains the CSS to hide the element) to it.
  • use ($styles): This syntax allows the anonymous function to access the $styles variable from the outer scope.
  • return $styles.$content;: This line concatenates the CSS styles with the content and returns it. As a result, the CSS that hides the .tdb-filter-bl_tag element will be included in the HTML output of the page.

6. How It All Works Together:

  • When a user visits a taxonomy archive page for the bl_category taxonomy, WordPress will check if the current term is a descendant of the term with ID 2981.
  • If it is, the function injects CSS into the page’s content to hide any elements with the .tdb-filter-bl_tag class.
  • This CSS is added directly into the content of the page, ensuring that it only affects the specific pages where the conditions are met.

7. Use Case:

  • This code is useful when you want to conditionally hide certain elements on specific taxonomy archive pages based on their position in a hierarchy. For example, you might want to hide certain filters or content sections on category pages that are children or grandchildren of a specific parent category.

8. Considerations:

  • Performance: This method injects CSS inline within the content, which can be fine for small tweaks but could become messy or harder to manage with more complex styles or larger sites.
  • Maintenance: If the class .tdb-filter-bl_tag changes or the taxonomy structure is altered, this code will need to be updated accordingly.
  • Scope: The CSS is added directly to the content, so it only applies to the content area of the page and might not affect elements outside the content, like headers or footers.

This approach allows for a quick and effective way to conditionally hide elements on specific pages in WordPress without needing to modify the theme’s stylesheet or templates directly.

Matt Pramschufer is a seasoned technology expert and co-founder of E-Moxie, where he specializes in transforming visionary ideas and business goals into thriving online ventures. With over two decades of experience in website design, development, and custom application creation, Matt understands that success requires more than just a visually appealing website or a well-built app. He combines proven methodologies with the latest internet technologies to deliver digital solutions that not only look great but also drive measurable growth.

Related Posts