Dynamic styling for categories in Oxygen repeater (core code method)

If you use the Oxygen repeater element to query post categories then you may have wondered how to style these categories individually - here's how by modifying the core code!

Disclaimer

Disclaimer: This method is not recommended. Please use this post to achieve the same result using jQuery or this post to achieve the same result using CSS only (favoured method).

The original solution to this was provided by Jarek Baran in the Oxygen Builder Community Facebook page.

To add a default dynamic class to all categories (to style category tags), you will need to modify the category-template.php file. But do not worry, it is fairly simple.

The steps

Step 1

Simply use your FTP client or manually locate the file called "Category-template.php" in your explorer (for me it was in Public_html>wp-includes>Category-template.php).

Step 2

Find the following snippet:

foreach ( $terms as $term ) {
		$link = get_term_link( $term, $taxonomy );
		if ( is_wp_error( $link ) ) {
			return $link;
		}
		$links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
	}

Step 3

Replace above snippet with the following:

foreach ( $terms as $term ) {
		$link = get_term_link( $term, $taxonomy );
		if ( is_wp_error( $link ) ) {
			return $link;
		}
		$links[] = '<a href="' . esc_url( $link ) . '" rel="tag" class="category-' .$term->slug . '">' . $term-&gt;name . '</a>';
	}

Step 4

Click "Save" and that's it!

Now everytime you query categories in a repeater for example, each category will have the class "category-{category name}" so you can now style them individually.

Disclaimer..again

Note: If there is a WordPress or Oxygen update, your changes in the category-template.php file will be overwritten with the original code so you will have to do the process again.

Want to be notified of new posts like this?

Sign up to be notified of new posts using the form below. Email addresses are used for the sole purpose of sending once-in-a-while email notifications and nothing else. I promise I don't spam!