1

I am trying to insert a "style" in single page of a category and its child categories.

.relatednews{display:none}.

I inserted the following code in functions.php of Child Theme, but it didn't work.

......................................................................

function post_is_in_descendant_category(){
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}
}
function hiderelated(){
if ( in_category( 168 ) || post_is_in_descendant_category( 168 ) ) {
   ?><style>.relatednews{display:none}</style><?php
}
}
add_action( 'wp', 'hiderelated' );

......................................................................

HELP ME! I need help to make this style work in categories 168 and its descendant categories?

1 Answer 1

1

A few changes to your code:

  • Changed the action hook to wp_head.
  • Removed the false 'pluggable' wrapping of your function.

if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        }
        return false;
    }
}

function hiderelated(){
  if ( in_category( 168 ) || post_is_in_descendant_category( 168 ) ) {
  ?>
  <style>
    .relatednews { display: none; }
  </style>
  <?php
  }
}
add_action( 'wp_head', 'hiderelated' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.