[WordPress] 外掛分享: Conditional Custom Fields Shortcode

首頁外掛目錄 › Conditional Custom Fields Shortcode
20+
安裝啟用
尚無評分
6074 天前
最後更新
問題解決
WordPress 2.7+ v0.5 上架:2009-07-29

內容簡介

一系列的短代码,可在页面或文章中(或通过小部件中的侧栏小部件-通过Section Widget!)使用自定义字段值,这将允许您将实际数据存储在自定义字段中,同时将演示内容存储在页面/文章正文中,将WordPress转变为了一款强大的CMS。继续阅读获取更多灵感。

基本用法
[cf "自定义字段名称"]

例如:
今日图片:
<img src='[cf "图片链接"]' />

将输出类似于<img src='http://thesite.com/something.jpg' />,假设帖子/页面具有自定义字段“图片链接”,其值为“http://thesite.com/something.jpg”。这样,您(或您的客户)更新页面时不会意外删除div,因为您不再需要修改页面/文章正文!

默认值
[cf "自定义字段名称" default="如果CF未定义,请显示此内容"]

例如:
如果您有任何问题,请联系[cf "support-email" default="[email protected]"]。

如果帖子/页面中不存在自定义字段“support-email”,则将在那里显示“[email protected]”。

简单模板
[cf "自定义字段名称" default="默认值"]...模板...[/cf]

例如:
价格:[cf "price" default="尚未提供定价信息。"] 美元%value% [/cf]

如果由于某种原因无法使用%value%作为占位符文本,您可以通过指定占位符属性来更改它。
例如:
价格:[cf "price" default="尚未提供定价信息。" placeholder="__value__"] 美元$__value__ [/cf]

多个值
它很聪明,能够做正确的事情。如果您的帖子具有以下自定义字段:

音乐会日期:1月14日
音乐会日期:1月28日
音乐会日期:2月2日

那么[cf "音乐会日期"]将输出Jan 14th,Jan 28th,Feb2nd,正是您所期望的。您还可以可选地提供分隔符以替换默认值(,)。
例如:
在以下日期中的任何一个到我们的音乐会:[cf "音乐会日期" separator=" / "]
结果为(=> Jan 14th / Jan 28th / Feb 2nd)

排序
[cf "自定义字段名称" sort="nosort|asc|dsc|random" type="string|int|integer|float|bool|boolean|date|auto"]

您可以使用sort属性(默认为nosort)控制如何对项目进行排序(当有多个项目时)。默认情况下,它将使用PHP的比较运算符进行比较(即尝试将其转换为数字值,如果失败则将其作为字符串进行比较)。但是,您可以通过指定type属性(默认值为auto)强制进行类型转换。

显示单个值
如果显示多个值不是您想要的,您可以使用[cf single="true"]强制只显示第一个值。将我们学到的内容结合起来,您可以通过[cf single="true" sort="random"]显示单个随机值。

摘要
[cf]短代码的语法:(默认值在括号内)
[cf "field-name" default="" placeholder="%value%" single="(false)|true" separator="," sort="(nosort)|asc|dsc|random" type="string|int|integer|float|bool|boolean|date|(auto)"](OPTIONAL) template[/cf]

外掛標籤

開發者團隊

⬇ 下載最新版 (v0.5) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「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 reading for more inspirations.
Basic Usage
[cf "custom field name"]

Example:
Picture of the day:

This would output something like , 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="[email protected]"] if you have questions.

[email protected] 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"]

You can control how it sort the items (when there’s more than one) using the sort attribute (default is nosort). By default, it would compare them using PHP’s comparison operator (i.e. try to cast them into numeric values, or compare them as strings if that failed). However, you can force a type cast by specifying the type attribute (default is auto).
Display Single Value
If display multiple values is not what you want it to do, you can use [cf single="true"] to force it to display only the first value. Combining what we have learned so far, you can display a single random value by [cf single="true" sort="random"].
Summary
Syntax for [cf] shortcode: (default values in parenthesis)
[cf "field-name" default="" placeholder="%value%" single="(false)|true" separator="," sort="(nosort)|asc|dsc|random" type="string|int|integer|float|bool|boolean|date|(auto)"](OPTIONAL) template[/cf]

Conditionals
Conditionals tags is what makes this plugin unique. Here is a list of them:
[if-cf-def "custom field name"]
Do this when "custom field name" is defined for this page/post
[/if-cf-def]

[if-cf-ndef "custom field name"]
Do this when "custom field name" is NOT defined for this page/post
[/if-cf-ndef]

[if-cf-eq "custom field name" "value"]
Do this when "custom field name" == "value"
[/if-cf-eq]

[if-cf-neq "custom field name" "value"]
Do this when "custom field name" != "value"
[/if-cf-neq]

[if-cf-lt "custom field name" "value"]
Do this when "custom field name" < "value" [/if-cf-lt] [if-cf-gt "custom field name" "value"] Do this when "custom field name" > "value"
[/if-cf-gt]

[if-cf-let "custom field name" "value"]
Do this when "custom field name" <= "value" [/if-cf-let] [if-cf-get "custom field name" "value"] Do this when "custom field name" >= "value"
[/if-cf-get]

More detailed documentations will be published soon. In the mean time, check out the following examples and the comments in the PHP code to get a rough idea.
Examples
[if-cf-get "deadline" "today" type="date"]
Submit your application here.
[/if-cf-get]

[if-cf-lt "deadline" "today" type="date"]
Sorry we regret that we cannot take anymore applications.
[/if-cf-lt]

[if-cf-def "discounted-price" "discount-expires" logic="and"]
This product is on discount, get it now for [cf "discounted-price" /] [cf "discount-expires" default="for a limited time"]before %value%[/cf]!
[/if-cf-def]

Please note: It seems that there is a serious bug in WordPress’ shortcode parser that prevents shortcodes from functioning correctly in certain occasions. The developers are hard at work to get this fixed before the 2.8.3 release. (See #10082) Therefore, please do not report any parser related bugs for the moment. (e.g. the shortcode is displayed on the actual page)

延伸相關外掛

文章
Filter
Apply Filters
Mastodon