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' );
Related Posts:
- remove_action on after_setup_theme not working from child theme
- Remove Actions/Filters added via Anonymous Functions
- Trying to use add_action and do_action with parameters
- Check if post is being published for the first time, or is an already published post being updated
- Perform an action when post is updated/published
- WordPress Theme Update Action?
- add_action in a function, is it possible?
- Add action hook conditionally – only when home.php in use
- How to add classes to images based on their categories?
- current_cat_ancestor Alternatives
- How can I tell if I’m on a login page? [duplicate]
- Add body class of category parent
- Change meta tags programatically
- Convert hyphen to underscore in permalinks
- How to hook into the quick edit action?
- Reuse variable in hook callback
- Assign category using custom field?
- How To Get WordPress Categories Link List?
- Changing post category from dropdown
- How to update feed only 2-3 times a week (for Feedburner email)?
- Manipulate Output of wp_list_something: select menu instead of li’s
- Make Categories a Dropdown Menu in Media Library
- Programmatically Create Category and sub Category
- Passing arguments to my function with do_action and add_action is not working
- Get the category name outside of the loop in category.php
- Is it possible to change any of the HTML/URL returned from the_category()
- Don’t delete a page if it holds users
- How do I add Bootstrap and LESS to my migrated WordPress site?
- How do I hide or remove ‘Category’ from wordpress breadcrumbs
- Ordering posts by publish date not working?
- Is there an alternative to get_template_directory_uri()?
- Wait a result before enqueue
- Categories as main menu items and subcategories as nested lists
- Multiple Loops Meta Data
- Function added using `add_action()` not being called
- Get category URL for current post
- jQuery does not work
- Need help “sanitizing” a custom function that pulls category slug into body class
- Override the WordPress core function wp_referer_field
- Two functions with different arguments and add_actions, but identical code
- Remove a category from a post when saving a new post
- How to fix get_the_category function returning incorrect slug?
- Return category name with & Ampersand doesnt work
- I can’t seem to install Font Awesome locally [closed]
- Scripts not loading when using the wp_enqueue_scripts action
- problem loading stylesheets to wp_head dynamically
- current_user_can comma list vs OR (||) list
- Add back in child theme what the parent theme removed with remove_action
- login_headertitle is deprecated since version 5.2.0
- How to properly refresh page after form action?
- 400 Bad Request – JavaScript App calling Custom wp-json endpoint
- Exclude Category ID in function
- Problem with calling custom function in a foreach loop
- “All posts” in the category widget
- Changing where my author box is printed
- Get Attachment Category Name
- Use add_action within template
- Problem with custom function when I go back with the browser
- Replace admin header logo with an image
- New checkbox in custom widget isn’t saving data
- template_redirect action only firing if logged in
- WordPress Categories: Function using custom SQL to return array of specific category IDs
- create function to call category name and slug
- Display ACF category image on archive and single template files
- How to use wp_enqueue_script properly?
- Using get_terms for custom taxonomy in functions.php
- Change category display name function
- Issue passing action class to nested function. Admin Columns
- Display link to category over featured image
- Does hook have an effect on increasing the page load?
- What is the earliest Hook a Script can use?
- If has action not working as expected
- post value to function with Ajax and jQuery
- Is_Page doesnt detect my page
- How do you insert code into the sidebar?
- How Do I Unhook This Parent Theme Function?
- Custom Function.PHP Code Not Working on One Site
- Adding new Category does not refresh the backoffice
- How to show only specific category post by user role without plugin and restrict all other cats
- use add_action in a shortcode (gravity form – WordPress)
- wp_footer hook causing text to show on bottom of page
- i can’t use wordpress functions in ajax loaded php file
- Graphic before title – Specific Category
- Which method is more correct for removing WooCommerce Extensions menu item?
- require_once() Causing categories and tag pages on dashboard to not refrsh
- True parameter but jquery register in header and not in the footer with wp_register_script
- different body classes for each category
- Sending Messages Back to the Template After Processing?
- previous_post_link inside of a function?
- custom COOKIE on custom page
- Settings in functions.php used by a plugin
- How to get tags and categories?
- How to Insert A List of Posts in A Category Written by the Author into the Author Archive
- Do something when user creates post (that’s pending)
- delete_term is not working properly with add_action()
- How to get variable from other function inside class function using add_action for Ajax call
- I am looking for a function to create category links in the wordpress loop for articles
- wp_update_post breaks my function
- is there a list of actions on WordPress
- Output parent/child categories and posts in that parent/child hierarchy