Textpattern CMS Template Tags

<txp:if_custom_field /> container tag, 4 attributes

if_custom_field

The if_custom_field tag is a conditional tag and always used as an opening and closing pair, like this…

<txp:if_custom_field>
...conditional statement...
</txp:if_custom_field>

The tag will execute the contained statements if one or more custom fields for a given article have content. The contents of a custom field can be displayed with the custom_field tag.

attributes

name="unset" 
  • The custom field name you wish to check.
value="unset" 
  • The custom field content you want to check for a match.
match="exact" 
  • How you wish your value to be tested. Choose from:
    • exact : (default) value text must exactly match the custom field
    • any : checks if any of the given comma-separated list of values occur anywhere in the custom field
    • all : checks if all of the given comma-separated list of values occur anywhere in the custom field
    • pattern : allows you to specify a regular expression in your value attribute to match against the custom field
separator="unset" 
  • If you wish to treat your custom field as a list of items — so that each item is a discrete entity and tested separately when using any or all matching — specify the delimiter that you use in the custom field. This attribute is ignored if using exact or pattern matching

genealogy

Version 4.3.0

  • Attribute val deprecated and renamed to value
  • Added the match and separator attributes
<txp:if_custom_field /> 3 examples

Display contents of custom fields

<txp:if_custom_field name="subtitle"> 
  <txp:custom_field name="subtitle" />
</txp:if_custom_field>

What it does…
Checks if a custom field has any content (at all) and display it.

Why you might do it…
Say, you are publishing book reviews on your site and you use custom fields to enter the author, title, publisher and year of publication (see example). Some of the books have a subtitle, others don’t so the conditional checks if the custom field you named “subtitle” holds any content and if it does, it will be displayed. If it’s empty, the field won’t turn up on the page.

The whole set of custom fields could look like this:

<p><txp:custom_field name="author" />: <txp:custom_field name="title" /> <br />
  <txp:if_custom_field name="subtitle"> 
    <txp:custom_field name="subtitle" /><br />
  </txp:if_custom_field>
published by <txp:custom_field name="publisher" /> in <txp:custom_field name="year" />.</p>

For a book that has a subtitle, this may be seen:

<p>Stephen Covey: The Seven Habits of Highly Effective People<br />
Powerful Lessons in Personal Change<br />
published by Simon & Schuster in 2002.</p>

For a book without a subtitle, this might be shown:

<p>J.R.R. Tolkien: The Lord of the Rings<br />
published by HarperCollins in 2004.</p>

Other tags used: custom_field

Check custom field value

A mood indicator:

<txp:if_custom_field name="mood" value="happy">
  <img src="/images/happy-face.gif" alt="" />
</txp:if_custom_field>
<txp:if_custom_field name="mood" value="sad">
  <img src="/images/sad-face.gif" alt="" />
</txp:if_custom_field>

What it does…
Checks the content of the custom field named mood to see if it matches the text “happy” or “sad”. Depending which one it matches determines which of the emoticons is displayed.

Why you might do it…
If you define a custom field “mood”, you can enter a word to indicate your mood while writing an article. You enter either “happy” or “sad”.

Use the tag with else

<txp:if_custom_field name="website">
  <txp:custom_field name="website" />
<txp:else />
  <p>Unfortunately, this band hasn't got a website.</p>
</txp:if_custom_field>

What it does…
If the custom field named ‘website’ has some content, display it, otherwise display a standard message.

Why you might do it…
If you publish music reviews and you’ve set up some custom fields for the band name, the album title and the band’s website. But not all bands have a website and you want to display a standard message if a band hasn’t got one.

Other tags used: custom_field, else