Skip Navigation

[Resolved] Assign different content templates to each post in a view

This support ticket is created 4 years, 2 months 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.

Our next available supporter will start replying to tickets in about 3.12 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Lara 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1754045

Tell us what you are trying to do?

Assign different content templates to each post in a view depending on the value of a select field.
How do I embed $view_id == 44 in the if-condition?

add_filter( 'wpv_filter_force_template', 'prefix_fixed_content_for_visitors', 99, 3 );
// Define our callback
function prefix_fixed_content_for_visitors( $template_selected, $id, $kind ) {
    if ( $kind == 'listing-element' ) {   // HOW DO I EMBED THE $view_id == 44 HERE ?
        // Only do something in that scenario
        $element = get_post_meta( $id, 'wpcf-elementtyp', true );  // VALUE OF THE SELECT FIELD
        switch( $element ) {
            case 2:// Tuesday
                // The Content Template for Tuesdays has an ID of 42
                $template_selected = 42;
                break;
            case 3:// Wednesday
                // The Content Template for Wednesday has an ID of 43
                $template_selected = 43;
                Break;
            case 4:// Thursday
                // The Content Template for Thursday has an ID of 44
                $template_selected = 44;
                Break;
            case 5:// Friday
                // The Content Template for Friday has an ID of 45
                $template_selected = 45;
                Break;
            case 6:// Saturday
                // The Content Template for Saturday has an ID of 46
                $template_selected = 46;
                Break;
            case 7:// Sunday
                // The Content Template for Sunday has an ID of 47
                $template_selected = 47;
                Break;
            default:// Monday
                // The Content Template for Monday has an ID of 41
                $template_selected = 41;
                break;
        }
    }
    return $template_selected;
}

I also thought about using this way to solve the problem:

<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>

But I am a little bit worried, that it will increase the loading time heavily.

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/toolset-hooks/
https://toolset.com/forums/topic/view-with-two-templates/

Is there a similar example that we can see?
In the first documentation, but without a view ...

What is the link to your site?
hidden link -> Testsite under development. It should become something else during the next days..

#1755319

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Lara

The wpv_filter_force_template filter is not intended to control the template used in the output of a View, it is used to override the template assigned to single posts or archives etc. (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template). It is not possible to use in the way you are hoping.

But your second suggestion is perfectly valid, and is the way I would have recommended doing this.

There is a little bit of extra overhead from checking the value of the custom field before inserting the template, but any method you used would have to use some similar logic to be able to differentiate the posts that should use one template rather than another.

#1756265

Hi Nigel,

many thanks for your answer. So the content between [wpv-conditional if="( $(wpcf-fieldslug) eq '2' )"] and [/wpv-conditional] will not be loaded, if wpcf-fieldslug is not 2 ? I once used it in another project in the following way ....

<wpv-loop>
[wpv-conditional if="( $(wpcf-fieldslug) eq '2' )"]
Nested View A
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-fieldslug) ne '2' )"]
Nested View B
[/wpv-conditional]
</wpv-loop>

.... and it slowed down the entire page heavily. Since then I suspected that, although it does not show the content, it still loads it. But maybe I am wrong ... It was just odd, that the loading time improved as soon as I removed one of those Views

#1756275

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Related to the possible nesting of conditional shortcodes my understanding is that the content within a conditional shortcode does get expanded, and this has been the source of performance issues.

However, we recently introduced major changes to avoid such issues with conditional statements. The will be included in the next releases of Views and Blocks (due in September, when exactly depends on how testing goes), so if you try using it now and it seems to perform okay I would go ahead with that solution, as the performance should improve further after the plugin updates.

#1756387

Many thanks Nigel 🙂
That are really awesome news.
Then I will go ahead with the conditional solution.
Thanks again

Kind regards
Lara