Skip Navigation

[Gelöst] Change color of every second item

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:

The issue here is that the user wanted to change the background color of every other item that is displayed in their view loop.

Solution:

This can be done by using the [wpv-item] shortcode with the index attribute odd and even.

Example

[php]

[wpv-item index=even]

[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]

[wpv-item index=odd]

[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]

[php]

Where you can assign different css classes to the odd and even items.

This support ticket is created vor 6 Jahren, 1 Monat. 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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 20 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von thomasR-9 vor 6 Jahren, 1 Monat.

Assistiert von: Shane.

Author
Artikel
#1134426

Tell us what you are trying to do?
I build a masonry grid for my elements. And now I want to change the color for every second item.

Is there any documentation that you are following?
No.

Is there a similar example that we can see?
No.

What is the link to your site?
<script src="hidden link"></script>
[wpv-layout-start]
[wpv-items-found]
<div class="masonry-grid">
<!-- wpv-loop-start -->
<wpv-loop>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-item">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]
</div>
</wpv-loop>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

URL: versteckter Link

#1134500

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

Thank you for contacting our support forum.

To get this clear, you want every other item to have this correct?

Please let me know.

Thanks,
Shane

#1134797

Hi Shane,

yes, I want to change the color of the every other item (#1 grey, #2 blue, #3 grey ...). If it is possible, I want a free space (about 55 px) between the first and the second column.

Best,
Thomas

#1135047

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

Now I thought you wanted something like red,blue,red,blue. Alternating colors essentially.

With this requirement it won't be quite possible, unless we know the classname or ID of each item that is created. In your case it would be something that is generated dynamically so we wouldn't be able to know the classes of each new item that is added.

Let me know if this is clear.

What we can do is change the color of each alternate item between 2 colors.

Thanks,
Shane

#1135151

Hi Shane, how can I change the color of each alternate item between 2 colors? Best, Thomas

#1135160

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

Take a look here.

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-item

This is the shortcode we use to do this.

So lets say you have your view.

<wpv-loop>
    [wpv-item index=even]
   HTML for even output
    [wpv-item index=odd]
   HTML for odd output
 </wpv-loop>

For the even and odd outputs you can give them different classnames and from thi you can assign them alternating colors.

Please let me know if this helps.
Thanks,
Shane

#1135373

Hi Shane, it works not like I want. I tried this
<script src="hidden link"></script>
[wpv-layout-start]
[wpv-items-found]
<div class="masonry-grid">
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=even]
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-item change-even">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]</div>
[wpv-item index=odd]
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-item change-odd">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]
</div>
</wpv-loop>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

And in the css-class I defined the background:
.change-even {
background-color: #ffffff;
padding: 13px;
}

.change-odd {
background-color: #e9e9e9;
padding: 13px;
}

But the result under is not changing colors:
versteckter Link

#1136111

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

Its because you're using 2 colums, so all the evens would naturally line up with each other.

Try using a 3 column layout.

Thanks,
Shane

#1136149

Hi Shane, I tried a 3-column layout, see versteckter Link
but it didn't work.
What can I do?
Best, Thomas

#1136224

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

I'm not able to see this page.

Would it be possible to make this page public so that I can see what it looks like?

Thanks,
Shane

#1136517

Sorry, now you can see it!

#1136849

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

Could you add this to the loop as well ?

[wpv-item index=pad]

Please let me know if this helps.
Thanks,
Shane

#1136858

Hi Shane,

here is the code:
<script src="hidden link"></script>
[wpv-layout-start]
[wpv-items-found]
<div class="masonry-grid">
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=even]
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 grid-item change-even">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]</div>
[wpv-item index=odd]
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 grid-item change-odd">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]
</div>
</wpv-loop>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

Where can I add your code?

Best, Thomas

#1136975

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Thomas,

Add it right before the closing </wpv-loop>

So it should be .


[wpv-items-found]
<div class="masonry-grid">
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=even]
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 grid-item change-even">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]</div>
[wpv-item index=odd]
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 grid-item change-odd">
[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]
</div>
[wpv-item index=pad]
</wpv-loop>
<!-- wpv-loop-end -->
</div>
[/wpv-items-found]

Please try this and let me know if it helps.
Thanks,
Shane

#1136976

Hi Shane, I copied your code and now my code looks like this:

<script src="hidden link"></script>
[wpv-layout-start]
[wpv-items-found]

<div class="masonry-grid">

<!-- wpv-loop-start -->

<wpv-loop>

[wpv-item index=even]

<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 grid-item change-even">

[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]</div>

[wpv-item index=odd]

<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 grid-item change-odd">

[wpv-post-body view_template="Loop item in Stimmen Alle Masonry"]

</div>

[wpv-item index=pad]

</wpv-loop>

<!-- wpv-loop-end -->

</div>

[/wpv-items-found]

[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

But it doesn't work!

Best, Thomas