The 2 different solutions in this tutorial enable you to change the background color, not background image, based on the category the post is assigned to. The code works in any WordPress theme.
In this tutorial, you’ll get 2 code snippets which enable you to change the background color :
- Using pure PHP code
- Using ACF with PHP code
The 2nd solution requires changing the background color via the edit category admin screen :
There are other ways to do this using PHP, jQuery or CSS however these 2 are amongst the most efficient methods.
Firstly, let’s take a look at a video which shows you exactly what the code in this tutorial enables you to do.
Video Demo
The video shows how you can set a default background color using the customizer which you can change on any category archive page. You can also set a default for the category page and posts in the same category if none added.
Here’s the code snippets for registered users :
Solution 1 : Uses Pure PHP Code
Both solutions use the theme_mod_$name filter.
Copy & paste the PHP code ( without the opening PHP Tag ) from the file named solution-12.php to the end of your child themes functions file. You can then Swap out the category id’s and/ore conditional tags in the code to suit your own requirements.
Solution 2 : Uses ACF With PHP Code
Step 1 : Copy & paste the PHP coe from the file named solution-2.php to the end of your child themes functions file :
Step 2 : Install ACF and create a new field named acf_color
using the following settings as a guide :
Was This Tutorial Helpful?