[WordPress] 外掛分享: Template Dictionary

首頁外掛目錄 › Template Dictionary
10+
安裝啟用
尚無評分
1958 天前
最後更新
問題解決
WordPress 4.5+ v1.6.1 上架:2017-08-14

內容簡介

Template Dictionary 是一個外掛,可用於創建由管理員編輯的模板變數。

這個外掛支援多語言,包含 Polylang 整合,也可以透過過濾器整合其他多語言外掛。

管理員頁面

這個外掛有幾個管理員頁面。

模板字典 - 查看、編輯和刪除模板變數值
設定清單 - 查看、編輯和刪除模板變數設定
新增設定 - 新增或編輯模板變數設定
編輯值 - 編輯值,可以從模板字典頁面訪問
匯出/匯入 - 將設定和值匯出為 XML 或從 XML 匯入

在模板中使用

可以透過呼叫 Template Dictionary 物件的 function get 在模板中使用值,而此物件可以透過 function TmplDict() 存取。

例如,若您將設定代碼設為 the_code,可透過以下方式使用此值:

TmplDict()->get( 'the_code' );

此 function 會傳回值。如需 echo 此值,可以使用 function eget。兩個 function 都有可選參數 $default,若管理員值為空,則回傳 / echo 的預設值為此參數的值。

您也可以直接透過代碼作為屬性存取值:

$value = TmplDict()->the_code;

使用 shortcode

可以使用帶有屬性code、default 和do_shortcode的 shortcode [tmpl_dict]。最後一個屬性指示是否在值上呼叫 function do_shortcode。

設定模板語言

可以透過在 wp-config 中設定常數 TMPL_DICT_DEFAULT_LANG 來設置預設的模板語言。此常數的預設值為當前 WP 地區的前兩個字母。如果您想在未來創建多語言網站,請適當地設置預設語言常數。

如果您有多語言網站,則需要使用過濾器 template_dictionary_language 設置當前模板語言。對於 Polylang 的使用者,此外掛已經包含整合,當前模板語言即為 Polylang 當前語言別名。

設定可用語言

如果您有多語言網站,還需要使用過濾器 template_dictionary_languages 設定可用語言。再次提醒,如果啟用了 Polylang,這些可用語言包含 Polylang 語言別名。

如果您的網站是單語言的,只有一種可用語言,即預設語言。

使用 JS 物件

可以定義 TMPL_DICT_JS_VAR_NAME 常數以生成 JavaScript 字典物件。在 wp_config.php 中新增此常數:

define( 'TMPL_DICT_JS_VAR_NAME', 'dict' );

然後可以在 JS 中使用它:

$('#some-element').text( dict.the_code );

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.6.1) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Template Dictionary」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

Template Dictionary is a plugin which can be used to create template variables editable by admins.
The plugin is multilingual ready. It includes Polylang integration, another multilingual plugin can be integrated with filters.
Admin pages
There are few admin pages for managing the plugin.

Template Dictionary – view, edit and delete the template variables values
Settings list – view, edit and delete template variables settings
Add setting – add new or edit template variables setting
Edit values – edit a value, this page is accessable from the page Template Dictionary
Export/Import – export settings and values to xml or import it from xml

Usage in template
You can use a value in template by calling function get of Template Dictionary object, which can be accessed by function TmplDict().
For example, if you set a setting with code the_code, you can use it’s value by:
TmplDict()->get( 'the_code' );

This function returns the value. If you need to echo the value, you can use function eget. Both functions have an optional argument $default, which is the default returned/echoed value, if the admin value is empty.
You can also get the value by accessing the code as property:
$value = TmplDict()->the_code;

Usage with shortcode
You can also use a shortcode [tmpl_dict] with attributes code, default and do_shortcode. The last attribute says, if the function do_shortcode will be called on the value.
Setup template language
Default template language can be set by setting the constant TMPL_DICT_DEFAULT_LANG in wp-config. Default value of this constant consists of two first characters of the current WP locale. It is important to set the default language constant properly if you plan to create your site multilingual in future.
If you have a multilingual site, you need to set current template language with the filter template_dictionary_language. For users of Polylang, it is already integrated in this plugin, the current template language is the Polylang current language slug.
Setup available languages
If you have a multilingual site, you also need to set available languages with the filter template_dictionary_languages. Again, if Polylang is activated, these available languages consist of Polylang languages slugs.
If your site is not multilingual, there is only one available language – the default language.
Use JS object
To generate JavaScript dictionary object, you need to define TMPL_DICT_JS_VAR_NAME constant. Add to wp_config.php:
define( 'TMPL_DICT_JS_VAR_NAME', 'dict' );

Then you can use it in JS:
$('#some-element').text( dict.the_code );

延伸相關外掛

文章
Filter
Apply Filters
Mastodon