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
- Display random categories on the front page (Finding and Editing Theme Functions)
- How to use the do_action () with parameter
- Perform an action when post is updated/published
- WordPress Theme Update Action?
- Extract image from content and set it as the featured image
- Do WordPress’ cron’s clean up expired transients?
- add_action in a function, is it possible?
- Using a private method as an action callback from within a class
- Refresh page after form action
- How to restrict actions and filters “properly” by conditions
- How to count number of functions attached to an action hook?
- Add action hook conditionally – only when home.php in use
- How to add classes to images based on their categories?
- Using add_filter() in Widgets
- 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
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Using get_terms for custom taxonomy in functions.php
- Fatal error: Call to undefined function add_action() – an untouched problem
- Php string not working in WordPress Functions.php (trying to fetch 1st category for each blog that post appears in the sidebar)
- Add class to Categories Widget
- How to Acheive the custom woocommerce category template
- How to hook into the quick edit action?
- Reuse variable in hook callback
- Call to undefined add_action() in theme’s functions.php
- How to add a rel attribute to images that contains their categories?
- How to set child post categories to parent post categories when updating parent post?
- Assign category using custom field?
- WooCommerce add_action hook results in 500 error
- How To Get WordPress Categories Link List?
- Automatically Add Specified Value to Attachment Metadata upon Upload
- Can’t Update function.php after writing short code
- 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
- How would go about if I just want a temporary function?
- Attach parent category template to all subcategories
- Passing arguments to my function with do_action and add_action is not working
- Pass parameters to function through an action
- Accepted arguments value in hook functions
- How to select a page within admin?
- How to manage arrays from custom functions stored in functions.php?
- exclude a category from a search on a specific page
- Get the category name outside of the loop in category.php
- How do I pass arguments for multiple functions hooked to a single action?
- Which action hook to use for function?
- Is it possible to change any of the HTML/URL returned from the_category()
- Possible to display shortcode based on the category?
- Unset Category if other Category is unset during post transition
- Modify a function without editing template
- Select pages by category
- Don’t delete a page if it holds users
- How do I add Bootstrap and LESS to my migrated WordPress site?
- Which action does wp_update_user triggers?
- Show Primary Category first when I display post categories
- How do I hide or remove ‘Category’ from wordpress breadcrumbs
- Ordering posts by publish date not working?
- How can I get my Script to work on the Login page?
- Trigger a custom function when option are saved in admin area
- Is there an alternative to get_template_directory_uri()?
- Reuse variable in hook callback
- Wait a result before enqueue
- Redirect to another page if the user is logged in when pressing again the login button on menu bar
- 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
- Difference b/w Simple function call & do_action call
- jQuery does not work
- Need help “sanitizing” a custom function that pulls category slug into body class
- allow only one post in specific category
- Override the WordPress core function wp_referer_field
- Set Taxonomy based on post status
- How to obtain the recent posts without their content in an efficient way?
- Setting a default text for excerpts of a particular category
- Do something when user creates post (that’s pending)
- add variable to actions/functions across different files (woocommerce)
- Removing all Category pages with one exception
- HELP: Code To Check Status And Write Debug Entry
- delete_term is not working properly with add_action()
- Get category of post inside save_post hook
- How to get variable from other function inside class function using add_action for Ajax call
- Function attached to cron job not running but will run if called manually
- I am looking for a function to create category links in the wordpress loop for articles
- Submit CF7 form programmatically with WP-Cron?
- Override categories with Pages (block theme)
- disable a specefic sidebar when user is log out
- wp_update_post breaks my function
- is there a list of actions on WordPress
- Remove Actions/Filters added via Anonymous Functions
- Hreflang Tags from the Most Recent Post Appearing in Category Page Headers