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