2
新手在這裏。我正在嘗試創建一個WordPress插件,並獲取以下代碼以顯示字體選擇的下拉列表。它的工作原理,但看起來錯了,沒有效率,改變數組的顯示值而不寫這些if語句的方法是什麼?替換數組值
$plugin_options = get_option('plugin_options');
$plugin_fonts_array = array('Chunk', 'Crimson', 'Deja Vu Sans', 'Deja Vu Sans Condensed', 'Deja Vu Mono', 'Deja Vu Serif', 'Deja Vu Serif Condensed', 'IM Fell French Canon', 'Junction', 'Lato Light', 'Lato Regular', 'Lato Black', 'League Gothic Condensed', 'League Gothic Regular', 'League Script Number One', 'Linux Libertine', 'Linux Libertine Display', 'Linux Libertine Mono', 'OCRB', 'Open Baskerville', 'Open Sans Light', 'Open Sans', 'Open Sans Semi Bold', 'Open Sans Extra Bold', 'Prociono', 'Raleway Thin', 'Source Sans Pro Extra Light', 'Source Sans Pro Light', 'Source Sans Pro', 'Source Sans Pro Semi Bold', 'Source Sans Pro Black');
foreach ($plugin_fonts_array as $font) {
if ($font == $plugin_options['dropdown1']) {
$plugin_front_font = $plugin_options['dropdown1'];
if ($plugin_front_font == 'Chunk') { $plugin_front_font = 'chunk'; }
if ($plugin_front_font == 'Crimson') { $plugin_front_font = 'crimson'; }
if ($plugin_front_font == 'Deja Vu Sans') { $plugin_front_font = 'DejaVuSans'; }
if ($plugin_front_font == 'Deja Vu Sans Condensed') { $plugin_front_font = 'DejaVuSansCondensed'; }
if ($plugin_front_font == 'Deja Vu Mono') { $plugin_front_font = 'DejaVuSansMono'; }
if ($plugin_front_font == 'Deja Vu Serif') { $plugin_front_font = 'DejaVuSerif'; }
if ($plugin_front_font == 'Deja Vu Serif Condensed') { $plugin_front_font = 'DejaVuSerifCondensed'; }
if ($plugin_front_font == 'IM Fell French Canon') { $plugin_front_font = 'imfellfrenchcanon'; }
if ($plugin_front_font == 'Junction') { $plugin_front_font = 'junction'; }
if ($plugin_front_font == 'Lato Light') { $plugin_front_font = 'latolight'; }
if ($plugin_front_font == 'Lato Regular') { $plugin_front_font = 'latoregular'; }
if ($plugin_front_font == 'Lato Black') { $plugin_front_font = 'latoblack'; }
if ($plugin_front_font == 'League Gothic Condensed') { $plugin_front_font = 'leaguegothiccondensedregular'; }
if ($plugin_front_font == 'League Gothic Regular') { $plugin_front_font = 'leaguegothicregular'; }
if ($plugin_front_font == 'League Script Number One') { $plugin_front_font = 'leaguescriptnumberone'; }
if ($plugin_front_font == 'Linux Libertine') { $plugin_front_font = 'linuxlibertine'; }
if ($plugin_front_font == 'Linux Libertine Display') { $plugin_front_font = 'linuxlibertinedisplay'; }
if ($plugin_front_font == 'Linux Libertine Mono') { $plugin_front_font = 'linuxlibertinemono'; }
if ($plugin_front_font == 'OCRB') { $plugin_front_font = 'ocrb10'; }
if ($plugin_front_font == 'Open Baskerville') { $plugin_front_font = 'openbaskerville'; }
if ($plugin_front_font == 'Open Sans Light') { $plugin_front_font = 'opensanslight'; }
if ($plugin_front_font == 'Open Sans') { $plugin_front_font = 'opensans'; }
if ($plugin_front_font == 'Open Sans Semi Bold') { $plugin_front_font = 'opensanssemibold'; }
if ($plugin_front_font == 'Open Sans Extra Bold') { $plugin_front_font = 'opensansextrabold'; }
if ($plugin_front_font == 'Prociono') { $plugin_front_font = 'prociono'; }
if ($plugin_front_font == 'Raleway Thin') { $plugin_front_font = 'ralewaythin'; }
if ($plugin_front_font == 'Source Sans Pro Extra Light') { $plugin_front_font = 'sourcesansproextralight'; }
if ($plugin_front_font == 'Source Sans Pro Light') { $plugin_front_font = 'sourcesansprolight'; }
if ($plugin_front_font == 'Source Sans Pro') { $plugin_front_font = 'sourcesanspro'; }
if ($plugin_front_font == 'Source Sans Pro Semi Bold') { $plugin_front_font = 'sourcesansprosemibold'; }
if ($plugin_front_font == 'Source Sans Pro Black') { $plugin_front_font = 'sourcesansproblack'; }
}
}
+1使用'array_key_exists()',而不是一個循環! – HamZa 2013-03-25 02:22:19
它已經好多了(和作品),謝謝。 將博客導出爲pdf時,將字體設置爲封面,標題和正文文本。 – Swissgeek 2013-03-25 03:33:34
@Swissgeek不要忘記檢查它作爲答案:) – HamZa 2013-03-25 08:56:30