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 Actions/Filters added via Anonymous Functions
- 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
- WordPress Theme Update Action?
- Extract image from content and set it as the featured image
- Do WordPress’ cron’s clean up expired transients?
- 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?
- How to add classes to images based on their categories?
- Using add_filter() in Widgets
- 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?
- 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?
- WooCommerce add_action hook results in 500 error
- Automatically Add Specified Value to Attachment Metadata upon Upload
- Can’t Update function.php after writing short code
- How would go about if I just want a temporary function?
- Attach parent category template to all subcategories
- 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
- How do I pass arguments for multiple functions hooked to a single action?
- Which action hook to use for function?
- 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
- Which action does wp_update_user triggers?
- Show Primary Category first when I display post categories
- How can I get my Script to work on the Login page?
- Trigger a custom function when option are saved in admin area
- Reuse variable in hook callback
- Redirect to another page if the user is logged in when pressing again the login button on menu bar
- Get category URL for current post
- Difference b/w Simple function call & do_action call
- allow only one post in specific category
- Set Taxonomy based on post status
- How to obtain the recent posts without their content in an efficient way?
- Running script based on Category
- add_action template_redirect not working for home page
- AMP – Change rel=”canonical” from functions.php [closed]
- Is there a way to prevent a function/method from being called outside a specific hook?
- Auto-Tweet if Type is ‘Status’ using OAuth
- Missing argument for the function
- Access category within rss2_head hook?
- issue with if/elseif in_array inside foreach loop display only one post
- Randomizing wp_tag_cloud() in child theme
- Adding code before post title with the_title produces weird results
- Add back in child theme what the parent theme removed with remove_action
- login_headertitle is deprecated since version 5.2.0
- add_filter to specific WooCommerce Category
- How to rewrite wordpress search to work on specific category
- Init action and refresh page after form action
- Image loading function not working on archive.php template
- Specify multiple categories for custom post template – FATAL ERROR
- Insert Content Before div#main from the functions.php File
- Problem in using Customizer
- How do I find the code executed when wp_head() is called?
- How to load several hierarchical categories from functions.php
- create function to call category name and slug
- Get slug of current category in functions.php
- Display a list of random terms from custom taxonomy with shortcode
- How to Override Page Template if URL matches query?
- Save_post – Warning: Cannot modify header information
- How do I trigger WP CLI DB export using a PHP function?
- If has action not working as expected
- Modifying a WordPress Plugin
- Is_Page doesnt detect my page
- Remove the ‘category’ url for one category type
- How do i tweak my wp Post title base on category of the post
- How to show only specific category post by user role without plugin and restrict all other cats
- Postback redirect through add_action is not triggered
- how to remove a tag in the_category function
- Menu and category highlighting for a single post
- Remove action hook from Class, understanding OOP
- How to change form action of wp-login page with a function
- Graphic before title – Specific Category
- WordPress hooks to call a function inside a construct
- Fatal error: Call to undefined function add_action() – an untouched problem
- Pass arguments to function class with do_action()
- What add_action reference should I be using or should I use do_action?
- How To Get WordPress Categories Last Update Date?
- Getting error while trying to use custom comment function
- Settings in functions.php used by a plugin
- function to return comma separated list of meta values
- Removing all Category pages with one exception
- HELP: Code To Check Status And Write Debug Entry
- How to get variable from other function inside class function using add_action for Ajax call