0

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></div><div 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.

  • 1
    breadcrumbs.php is not a WordPress core file. Is it part of a theme/plugin? – gmazzap Mar 9 '14 at 15:14
  • 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. – s_ha_dum Mar 9 '14 at 15:34
0

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.

  • I've got your suggestions and I appreciate them a lot, but this is a "fragile" way to accomplish what I'd like to do. I'm doing it with a plug-in, so it needs to be independent from theme or other customization (think about single_cat_title going to be used in somewhere else in the page after a customization). – kekko12 Mar 9 '14 at 16:16
  • 1
    Yes, I know. It is quite fragile. I stated that in the answer. What you are doing is theme and plugin dependent. Without having the details of the theme and plugins involved these are the best I can think of. – s_ha_dum Mar 9 '14 at 16:19
0

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');

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.