Skip Navigation

[Resolved] Content Template Name and Slug

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

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)

This topic contains 8 replies, has 3 voices.

Last updated by stevenT-5 2 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#2144353

Hi, I want to conditionally display widgets based on page template. We have this conditional code:

is_page_template( 'about.php' )

I create a page template using Tootsets. There's a pretty name give to it. It seems Toolsets creates virtual page templates without a physical file on the server such as page.php. Problem is that I need to know the actual template name in order to hook the conditional display to, like my_custom_template.php

Please advise and thank you.

steven

#2144987

Problem is that I need to know the actual template name in order to hook the conditional display to, like my_custom_template.php
Hello, it sounds like you need to be able to programmatically determine which Content Template will be applied to a post, so you can use that to determine whether or not to display some widgets on the page. The function is_page_template will not really help here because Toolset does not manipulate the selected PHP template. Instead, it hooks into the_content() to render the Content Template inside that selected PHP template. In other words, if a page had the about.php template before Toolset, it still uses the about.php template now. That should be unchanged when a Content Template is applied to the page. I suspect the function is_page_template('about.php') should be true now if it was true before.

If you need to display widgets conditionally based on the Content Template which will be applied to a page, you can use wpv_content_template_for_post as in the following example:

// Conditional logic for displaying widgets based on the Toolset Content Template applied to a post
// Support forum reference: https://toolset.com/forums/topic/content-template-name-and-slug/
global $post; // the global post, current page for example
$template_id = apply_filters( 'wpv_content_template_for_post', 0, $post ); // get the ID of the Toolset Content Template for this page
if (isset($template_id) && !empty($template_id)) {
  // The current post will be displayed using the Toolset Content Template with ID $template_id.
  // You can use $template_id to display widgets conditionally here.

} else {
  // The post will not be displayed using a Toolset Content Template. 
  // You must use other conditional code here.

}

This code is designed to work in Toolset Blocks 1.5+ or Toolset Views 3.5+. Content Template IDs can be found in the dashboard at Toolset > Content Templates. You should be able to use the Content Template ID to determine whether or not to display widgets, if I understand your problem correctly. Does this help, or have I misunderstood?

#2145013

Thanks for the detailed explanation and reference code, Christian. What you have though makes sense, it doesn't directly solve my issue.

I use Toolset to create a post type. Then I also create a Content Template layout to display entries under this post type. So I want the system to display certain widgets only for posts under this content type and content template. I'm using another plugin (Cobalt Apps) to configure widget display conditionally and not editing the functions.php file for this task. Cobalt Apps plugin allows the use of most WP conventional conditional tags to display widgets and content:
https://codex.wordpress.org/Conditional_Tags#Conditional_Tags_Index

So for the setup I have, it would work only if I can use [is_page_template( '' )] to conditionally display widgets I want. Hope this helps your understanding my particular situation.

Thank you

#2145097

So for the setup I have, it would work only if I can use [is_page_template( '' )] to conditionally display widgets I want. Hope this helps your understanding my particular situation.
Okay this all makes sense. I understand that you have access to a limited system for conditional logic based on the conditional tags native to WordPress. Unfortunately none of those native conditional tags will help you determine which Content Template is applied to a post...at least not directly.

I suspect you could create additional PHP page template files, one corresponding to each Content Template that might be applied to your posts. So you would duplicate the PHP page template file(s) currently applied to the posts, with a file name that corresponds to the desired Content Template name. You would not make any changes to the code in those new PHP template files, unless the code in the original template changes. Once you have created these cloned PHP template files, you would then edit each post and select the new page template with the file name that corresponds to the Content Template that will be applied to the post. Then I think you would be able to use the is_page_template function like you have described to determine whether or not to display specific widgets on each post, since the page template name now reflects the Content Template that will be applied to the post.

It's redundant, and you would need to remember to always update the cloned templates if the original templates change, but it should allow you to use the available conditional tags to display widgets conditionally on native Pages.

#2147277

Christian, your proposed solution makes sense can could work. The tricky part is that the post type I create has entries submitted frequently by site users. So it may not be practical having to set post template frequently on an one going basis. If it were for a few core pages on the site, the solution could work. But I will end up with hundreds of posts over time.

One idea is that I can batch assign the posts with a category to which I can hook the widget display condition with.

#2151021

Shane
Supporter

Languages: English (English )

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

Hi Steven,

As Christian is currently on vacation, I will be filling in for him.

One idea is that I can batch assign the posts with a category to which I can hook the widget display condition with.

This would work because here you will have an identifier for each posts and you can use this to determine which posts gets the widgets and which don't.

Also our content templates also allows for conditionals on the post type assignment so you can even use this to check if the posts has a particular taxonomy assigned and then assign a content template to it based on the taxonomy.

Thanks,
Shane

#2154099

Hello Shane -
Since you brought up another idea:
"Also our content templates also allows for conditionals on the post type assignment so you can even use this to check if the posts has a particular taxonomy assigned and then assign a content template to it based on the taxonomy."

The above sounds interesting though I'm not quite sure what you mean or how that works on content templates. Can you please elaborate this idea with more detail?

#2155397

Shane
Supporter

Languages: English (English )

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

Screenshot 2021-08-30 at 9.02.28 AM.png

Hi Steven,

If you're creating your content template using the Gutenburg Editor then under the Usage tab for the content template you will see the list of Post Types, from there you should see the "set conditions" button. See Screenshot

With this you are able to assign specific content template to only specific posts in the post type based . Only posts that meet the conditions that you set will get the template assigned.

These conditions can be based on Taxonomy terms selected, custom field values etc.

Please let me know if this helps.
Thanks,
Shane

#2156363

Thanks for the explanation, Shane. I see that we can have conditional settings for a category and then conditional that category to display sidebar widgets. I think this should work. Will experiment on this and let you know otherwise.

Again, thank you all for the help.

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