Skip Navigation

[Resolved] Custom template chosen with custom field value?

This support ticket is created 8 years 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 2.22 hours from now. Thank you for your understanding.

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

Supporter timezone: Europe/Madrid (GMT+02:00)

This topic contains 4 replies, has 2 voices.

Last updated by garyC-4 8 years ago.

Assisted by: Juan.

Author
Posts
#437107

Hi,

I have a custom post type with a custom content template. Is it possible to have 2 content templates and choose between them based on the value I generate for a custom field value?

Basically, I have TV shows legally that I made a custom post type for. I want to have a template for when they are posted but not acting and another for posted AND active. Since I post the shows before I record them.

I have already made the content template for the shows that are posted but not active yet (pending). But I have to work out how to have another template for when I switch them over to active.

The way I have it is quite nice already so I am happy with how things are going. Just need to solve this last challenge then I'm good to go. =)

~Gary Cook.

#437276

Juan
Supporter

Timezone: Europe/Madrid (GMT+02:00)

Hi Gary

Thanks for the feedback

We do not have a GUI for conditionally applying a Content Template to a single post type based on conditions, and I can imagine that, although very helpfull, having such a thing would be maybe a usability problem. You know, there are too many condition sources (field values, autor, time, post relationships, taxonomies, combinations of some of those, etc etc) that in the end it would be quite difficult to set. As I sometimes like to point out, having a GUI for something doe snot necesarily makes it easier to use.

But on the other hand, we do have an API that you can use for this. We have a filter that is precisely in place for this kind of usage:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_force_template/

That filter basically checks that we are using a Content Template, checks the post that is involved, and identifies the situation (in your case, a single post of a given post type), and lets you modify the Content Template that will be finally applied. Think of this as a way to take a situation and alter which Content Template will be used, which sounds to me exactly what you are asking for.

In your case, I would do something like this (note that I did not test the code, I am just thinking aloud and you might need to adjust it):

add_filter( 'wpv_filter_force_template', 'prefix_select_content_template_by_active_status', 99, 3 );
function prefix_select_content_template_by_active_status( $template_selected, $id, $kind ) {
    if ( $kind == 'single-your-post-type-slug' ) {
        $act_status = get_post_meta( $id, 'your-field-slug' );
        if ( $act_status == 'option_one' ) {
            $template_selected = 123; //Adjust to the Content Template ID that you want to use in this case
        }
    }
    return $template_selected;
}

Something like that should do the trick. Again, note that the key word is "like that", but this should give you a starting point to play with the thing.

Hope it helps.

Regards.

#437546

I really appreciate the help but where do I put this? LOL

Sorry I am new to thi plugin.

Although, I know some PHP etc so I should be able to edit your code so it works for my use case.

#437688

Found the right PHP to inject this into and edited to suit my use case.

I don't think this is working. I have tried it with a simple A = A so A. It worked but not conditionally.

I could select between custom template 1 or 2 but they would always be the same for the custom post type. Always 1 or 2 but never some wit 1 and some 1 2 depending on my custom post field.

#437718

Ok, for some reason I needed to add this.

$act_status2=reset($act_status);

Not even sure why myself but it worked. Maybe an issue with the string being a string even though it was a number.

Thank you very much for your help.

Will be upgrading soon! =)

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.