filter single_cat_title avoiding the breadcrumbs

I'm developing a simple plug in and I need to filter single_cat_title in category page adding some html in every category page.

Just doing this:

function subtitleCategory ($title) {
    return $title."/div/div/divdiv class=\"container_24\"h2 id=\"category-subtitle\"TEST1/h2/div";
}

add_filter('single_cat_title', 'subtitleCategory');

I get what I need. But I noticed that this filter also apply to the breadcrumbs (there is a call in breadcrumbs.php) and this means twice in the same page (not what I need).

Do you know if there is a condition that can be applied to filter just the title and not the breadcrumbs? I tried in_the_loop, is_category, is_archive, is_tax etc etc without success.

--edit-- Yes, breadcrumbs.php is a theme file and you should not take it in consideration. I'm trying to achieve an admin panel where your can put some text (with html) for every category and this text will be displayed just after the title in the category page. I'm trying to do this with a plug in and, for this reason, this can't be coupled with any theme dependency.

Topic breadcrumb filters categories Wordpress

Category Web


Another approach is to use add_filter before single_cat_title('Category: '); and then use remove_filter.

Example:

add_filter('single_cat_title', 'subtitleCategory');
single_cat_title('Category: ');
remove_filter('single_cat_title', 'subtitleCategory');

Yes, there is probably a way to do this but it is going to be theme/plugin dependent. Without knowing the details I don't think a good answer is possible. However, there are a couple of general case solutions.

The first is to add your callback on a hook that runs after the breadcrumb code.

function subtitleCategory ($title) {
  return $title."</div></div></div><div class=\"container_24\"><h2 id=\"category-subtitle\">TEST1</h2></div>";
}
function hook_in_after_breadcrumb($qry) {
  add_filter('single_cat_title', 'subtitleCategory');
}
add_action('some_hook','hook_in_after_breadcrumb');

I don't know what some_hook should be as that is theme/plugin dependent.

You could count your instances that the filter fires and target one:

function subtitleCategory ($title) {
  static $c = 0;
  $c++;
  if (2 === $c) {
    return $title."</div></div></div><div class=\"container_24\"><h2 id=\"category-subtitle\">TEST1</h2></div>";
  }
}
add_filter('single_cat_title', 'subtitleCategory');

A change of theme or a plugin could break that somewhat cludgy, manual solution though.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.