Sauter la navigation

Assigning Templates to Posts Conditionally dans WordPress

Toolset allows you to set conditions under which Content Templates display for certain posts.

Let’s say you have a WooCommerce store for selling shoes and you have product categories for “Trainers” and “Heels”. You can assign a different template to display single trainer shoes and another one for heels shoes.

Template for displaying shoes in the “Trainer” category

Template for displaying shoes in the “Heels” category

Steps for assigning a template conditionally

  1. Create a new Content Template or edit an existing one.
  2. Under the Usage section in the right sidebar, make sure the template is assigned to a post type.
  3. Click the Set conditions link next to the post type’s name.
  4. Set your conditions using the dropdown menus in a pop-up dialog that appears. You can select conditions based on the available fields and taxonomies for the selected post type.
Dialog for setting up a conditional display for a Content Template
  1. When done setting up the condition(s) click the Accept button.

Setting the template priority

Sometimes, you might conditionally assign multiple templates to the same content. In this case, you need to set the template’s priority:

  1. Expand the Priority section in the right sidebar.
  2. Use the Template Priority field to set the desired priority for the conditional template. Higher number means higher priority.
Setting the priority of a Content Template

Conditions based on product’s stock status

You can set up the conditions based on the stock status of a product. Currently, you need to manually enter the value you want to check against.

Use the following values when setting up conditions based on stock status:

  • instock – is true when the product is in stock
  • outofstock – is true when the product is out of stock
  • onbackorder – is true when the product is on backorder

For stock status to work, you need to manually enable the stock management option for all relevant products.

Updated
avril 14, 2021