V
V
Vladimir2020-04-21 10:40:02
css
Vladimir, 2020-04-21 10:40:02

How to automatically (by styles, script) set color only for numbers in an article?

I have a site on Wordpress. Tell me, is it possible to somehow implement (using styles or a script) so that the numbers (just numbers) in the article are automatically painted in the color I set? That is, change the main color of not the entire text, but only the numbers, and if necessary, so that I can change the color of individual numbers to another one that is different from the main one.
Is it possible to do so, and how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Morev, 2020-04-21
@SeaInside

Hello!
Hang the filter on the_content, wrap the numbers in a span with the desired class in the filter using a regular expression, and set the styling in the CSS class.
(I haven't tested it, but it's probably true).

add_filter('the_content', function($content) {
  return preg_replace('~(\d+)~', '<span class="colored">$1</span>', $content);
});

.colored {
  color: red;
}

The above code will wrap all natural numbers, including as part of other strings - this is rarely what you need, but here you know better how it will be right, edit the regular expression for your tasks.
If there are some separate cases, then look towards preg_replace_callback - there you can check the occurrences of something specific and decide what to do with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question