I'm working on a site, largely managed by categories. Two of the categories have apostrophes in them... ex. "Joe's Place" and "Tina's Lounge". (Actual titles changed, these are examples...) The category slugs are "joes-place" and "tinas-lounge".

The customer asked for a breadcrumb on the site... Unfortunately the breadcrumb call fails whenever the category has an apostrophe in its name. The failure occurs at the call to get_cat_ID. Oh, and when the breadcrumb fails, the whole page fails. Here is a fragment of my breadcrumb code...

if ( is_category() )
            // single_cat_title() Displays or returns the page title if a category
            // or tag archive is queried. 
            $catTitle = single_cat_title( "", false );   
            echo '<!-- $catTitle:'.$catTitle." -->";  // troubleshooting line... 
            $cat = get_cat_ID( $catTitle );
            echo '<!-- $cat: '.$cat."-->";   // troubleshooting line
            echo "<li>  ". get_category_parents_custom( $cat, TRUE, ' &raquo; ' ) ."</li>";

get_cat_ID simply returns a 0. Uh-oh... when I look close at my troubleshooting code, I can see that $catTitle returns Joe&#8217;s Place

Ouch.. Its really not clear on exactly what format is required where the apostrophe is concerned. When I dig further and further in codex core, I can see get_cat_ID calls a function get_term_by() that warns "$value is not HTML-escaped for the 'name' $field. You must do it yourself, if required." I'm assuming that's my issue. but, what does it mean to "HTML-Escape" a string?

I tried $catTitle2 = htmlspecialchars_decode($catTitle); no go there.
I tried $catTitle2 = esc_html($catTitle); no go there. In both of those previous functions, the string never changes a bit.

I tried manually introducing the string "Joe's Place" to the call, and WOW, that totally worked.

In the meantime I will do a simple string replace function, but that seems like such a hack.

Is there a php or wordpress function that will convert the string &#8217; back to a simple apostrophe? What is the right way to fix this problem?

Update: I stumbled upon this posting: My quick fix consists of a bit of code suggested there with a small modification:

     $catTitleCorrected = html_entity_decode(str_replace("&#8217;","'",$catTitle)); 

You can use this. May it will help you.


Converts named entities into numbered entities.


So it seems that your final aim is obtain the current category id to use in a get_category_parents_custom function and you are using single_cat_title to get current cat name an pass it to get_cat_ID to get current category id.

I think you are making your life harder, because you can use get_queried_object_id to get the current category id when is_category() is true, or you can access the category object using get_queried_object and then make use of category ID and name form that object. Example code:

if ( is_category() ) {
  $cat = get_queried_object();
  // if you also need category name uncomment next line
  // $catTitle = $cat->name;
  echo "<li>  " . get_category_parents_custom( $cat->term_id, TRUE, ' &raquo; ' ) . "</li>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.