Skip Navigation

[Résolu] Selecting background color of a post in View loop, based on a custom field

This support ticket is created Il y a 3 années et 1 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par alexG-4 Il y a 3 années et 1 mois.

Assisté par: Waqar.


I know it is possible to select a background image to display in a view loop, as explained here

But I wish to select a background COLOR rather than an image, based on a custom field of the post type being displayed.

I can't that this is possible using the View Block. Is that so, or am I missing something?




Hi Alex,

Your observation is correct and in the block settings, there is no option to set the dynamic background color from the custom field.

But you can use a 'Fields and Text' block to insert some custom CSS code that uses the custom field value.
(please make sure to use 'HTML' mode)

For example, suppose you have a color type custom field 'post-color', and the container block where you'd like to show this dynamic background color has a class 'special-container'.

The CSS code, in this case, will look like this:

.special-container {background: [types field='post-color'][/types];}

I hope this helps and please let me know if you need any further assistance around this.



Hi Waqar

Thanks for that suggestion.

Unfortunately, I'm displaying several posts at once, so the background colour for ALL of them ends up being that of the last one to be rendered.

One option would be to create a single-colour image for each post and use the background image mechanism, but that's beginning to make it more trouble than it's worth.



Thanks for writing back.

In case of multiple items coming from the view, you can extend this workaround further.

1. You can wrap the contents of the individual post/loop item inside a "Container" block. And in the ID field of this container, you can insert 'special-container-[wpv-post-id]' to dynamically generate the unique ID using the current post's ID.
( screenshot: lien caché )

2. And in the loop, you can include the 'Fields and Text' block, to add the dynamic CSS code for each item's unique background, like this:

#special-container-[wpv-post-id] {background: [types field='post-color'][/types];}

This works on my test website.



Thanks so much Waqar.

My issue is resolved now. Thank you!

Ce ticket est maintenant fermé. Si vous êtes un client de Toolset et que vous avez besoin d'aide, veuillez ouvrir un nouveau ticket d'assistance