get_cat_ID returns 0 if used with Polylang
-
Hi everyone. I’ve just started to use WordPress and am working on my own theme. Since my page has to support multiple languages I’ve decided to use the Polylang plugin. Additionally I’ve added a category and its translation in the backend. Let’s call the category names “Services” (for English) and “Dienstleistungen” (for German) So far, so good.
Now I want to write a function
do_something($catname)
that generates some HTML code based on the given category and current language. Thus it’s called likedo_something("services")
in the theme (thus hardcoded parameter) and should use the correct translated category in its logic.For that purpose I’ve wanted to use
get_cat_ID($catname)
orget_term_by('name', $catname, 'category')
to get the actual ID and get the translated category ID by callingpll_get_term($cat_term_ID)
. If the page language is English everything works fine, bothget_cat_ID
andget_term_by
return the correct ID for the parameter “services”. As soon as I switch to German (with Polylang’s Language switcher), both return 0 resp. null. Thus I have no chance to callpll_get_term
if the language is switched to something different than English. Maybe I am missing something or have a wrong understanding of these functions. For me it isn’t clear why something likeget_cat_ID("services")
doesn’t work if the page is set to another language.
- The topic ‘get_cat_ID returns 0 if used with Polylang’ is closed to new replies.