Conditional Custom Fields Shortcode
A series of shortcodes for using custom field values in pages or posts (or in sidebar widgets – via Section Widget !), which would then allows you to take store the actual data in custom fields while storing the presentation in the page/post body. This transforms WordPress into an awesome CMS. Keep
A series of shortcodes for using custom field values in pages or posts (or in sidebar widgets – via Section Widget !), which would then allows you to take store the actual data in custom fields while storing the presentation in the page/post body. This transforms WordPress into an awesome CMS. Keep reading for more inspirations.
Basic Usage
[cf "custom field name"]
Example:
Picture of the day: <img src='[cf "picture-link"]' />
This would output something like <img src='http://thesite.com/something.jpg' /> , assume the post/page has the custom field “picture-link” and its value is “http://thesite.com/something.jpg”. That way, you (or your clients!) would never accidentally take out a div when updating your page because you won’t have to touch the page/post body anymore!
Default Values
[cf "custom field name" default="display this if the CF is undefined"]
Example:
Contact [cf "support-email" default="support@mycompany.com"] if you have questions.
support@mycompany.com will be displayed there if the custom field “support-email” is not present in that post/page.
Simple Template
[cf "custom field name" default="default value"]...template...[/cf]
Example:
Price: [cf "price" default="Pricing information not available yet."] USD$%value% [/cf]
If you cannot use %value% as your placeholder text for some reason, you can change it by specifying the placeholder attribute.
Example:
Price: [cf "price" default="Pricing information not available yet." placeholder="__value__"] USD$__value__ [/cf]
Multiple Values
It’s smart enough to do the right thing. If your post has the following custom fields:
- concert-date: Jan 14th
- concert-date: Jan 28th
- concert-date: Feb 2nd
Then [cf "concert-date"] will output Jan 14th,Jan 28th,Feb2nd , exactly what you would expect. You can also optionally provide a separator to replace the default (,).
Example:
Come to one of our concerts on the following dates: [cf "concert-date" separator=" / "]
(=> Jan 14th / Jan 28th / Feb 2nd)
Sorting
[cf "custom field name" sort="nosort|asc|dsc|random" type="string|int|integer|float|bool|boolean|date|auto"]