Skip Navigation

[Resolved] View with multiple templates

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View of custom posts. I have a custom field applied to those posts. Depending on the value of that custom field, I would like to apply a different Content Template to each item in the Loop.

Solution:
Use conditional HTML to test the value of the custom field, and show or hide the corresponding Content Templates.

[wpv-conditional if="( $(type-of-post) eq '1' )"]
  [wpv-post-body view_template="template-1-slug"]
[/wpv-conditional]
[wpv-conditional if="( $(type-of-post) eq '2' )"]
  [wpv-post-body view_template="template-2-slug"]
[/wpv-conditional]
[wpv-conditional if="( $(type-of-post) eq '3' )"]
  [wpv-post-body view_template="template-3-slug"]
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 1 month ago. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Charles 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1124108

Support,

Please see attached image. The output on the right circled in red is produced from the current view and its content template which is assigned to all the posts. The image inserted on the left is an example of what I would like to incorporate into the mix. It's a post with an image to an external link. Is there a way to modify the current view to have two views; either conditionally or a separate or view?

Note this is a screenshot of the view with the image circled in blue laid on top to achieve my goal.

Thanks,
Charles

#1124595

Sorry, I can't see an attachment. Can you add the image to your next reply?

#1124609

Image too large to attach. Can be viewed here hidden link

Thanks!

#1124690

Okay I see now thank you. It looks like you want some results in a View to be displayed using one template, and other results in the same View to be displayed using another template. This can be accomplished in different ways depending on how you determine which items receive which template. Have you reviewed this documentation?
https://toolset.com/documentation/user-guides/digging-into-view-outputs/

Check the section "Loop parameters & wpv-item : wrap, pad, pad-last, index" for specific information about how to use wpv-item and index to specify different output for different elements of a loop, in a repeating pattern.

Another option is to use conditional HTML in your View's Loop. For example, if you want to apply Template #2 to all results having a custom field value of "2", but apply Template #1 to all other results, you could do something like this:

<wpv-loop>
[wpv-conditional if="( $(wpcf-fieldslug) eq '2' )"]
[wpv-post-body view_template="CT #2"]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-fieldslug) ne '2' )"]
[wpv-post-body view_template="CT #1"]
[/wpv-conditional]
</wpv-loop>

Once you have the markup in place, you can use the CSS editor in the View's Loop panel to add any necessary CSS.

*edit - updated to fix code formatting

#1124866

Worked like a charm. I used the select field and coded like this:

      <wpv-loop>
        <div class="col-lg-3 col-md-4 col-sm-6 col-xs-12 grid-item">
          [wpv-conditional if="( $(type-of-post) eq '1' )"]
            [wpv-post-body view_template="Loop Item in Main View"]
          [/wpv-conditional]
          [wpv-conditional if="( $(type-of-post) ne '1' )"]
            [wpv-post-body view_template="main-view-ad"]
          [/wpv-conditional]
        </div>
      </wpv-loop>

Is it possible to have a 3rd and 4th template if desired?

Thanks,
Charles

#1125376

Yes, that's possible. In your View's Loop, you will copy and paste a conditional code block for each template. All the conditionals should test if the type-of-post value is equal to something, since the options are no longer only 1 or 2.

    [wpv-conditional if="( $(type-of-post) eq '1' )"]
      [wpv-post-body view_template="template-1-slug"]
    [/wpv-conditional]
    [wpv-conditional if="( $(type-of-post) eq '2' )"]
      [wpv-post-body view_template="template-2-slug"]
    [/wpv-conditional]
    [wpv-conditional if="( $(type-of-post) eq '3' )"]
      [wpv-post-body view_template="template-3-slug"]
    [/wpv-conditional]

We have documentation for conditional HTML here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#1125424

Perfect!

Thanks,
Charles