WPML
Status
Open

Overview of the issue

The native WordPress function has_category does not work for translated posts.

An example of this is as follows:

  1. We have a category with the English slug “test” and its French translation “test-fr”.
  2. This category is assigned to posts.
  3. When you invoke the has_category(”test”) inside a loop in your theme’s single.php file, it returns true for the default language but false for the translation.

Workaround

Define a new filter in your theme’s functions.php file:

function wp_has_category ($category) {
$category = get_term_by('name', $category, 'category');
return has_category($category);
}
add_filter('wp_has_category', 'wp_has_category', 10, 1);

Replace the has_category function with this new filter:
apply_filters( 'wp_has_category', 'test' )

This filter takes the translated category based on the name of original one and checks if the translated category is assigned to a post.

All known issues →