WordPress Conditional Content

How to use

You can place conditional content between if shortcodes, and use its attributes to define conditions. The content between the shortcodes will only be displayed if all defined conditions are met.

Defining conditions

Conditions are defined by setting the attributes of the shortcode in the following format:

A basic example:

The text in this example will only be displayed if the current URL has a GET paramater of “product-type” with the value “shoes”.

Available condition types are:

  • qs – Match on the key/value pair of the defined query string/GET parameter. Query string conditions are formatted like this:

  • referrer – To check on the current referrer. The condition will be true if HTTP_REFERER contains <value>. The match doesn’t need to be exact, so if a user arrives from google and <value> is set to “google.com” the condition will be true.

  • role – Matches the current user’s role. The match needs to be exact so “admin” will not match “administrator”. Use role with and empty value if you want to match users that are not logged in.

Examples

Display content based on query string:

[if qs=”utm_source:partner-site”]
This content is only displayed if the current URL contains a GET paramater ‘utm_source’ with value ‘partner-site’
[/if]

Display content based on referrer:

Display content based on user role:

Setting multiple conditions and combining condition types:

Matching with `exact` or `contain`

By default query string conditions are matched on the exact value as defined in the shortcode. For looser ‘wildcard’ matching you can add a match attribute with the value contain:

This will match both ?product-type=cashmere-sweater and ?product-type=cashmere-coat

Matching on multiple values

You can have a condition match on multiple values by using the semicolon as a seperator when defining allowed values. Example:

Nesting `if` statements

You can nest statements but you have to use iteration. This has to do with the limitations of the built-in shortcode API.

You can nest up to if4 (4 levels).

0

评论0

 
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录