add_meta_box()函數(shù)是被用來在文章編輯等頁面添加一個(gè)設(shè)置的區(qū)域的函數(shù)。
創(chuàng)建的文章類型默認(rèn)的僅有標(biāo)題、作者、分類、標(biāo)簽、日期和評(píng)論,這些也許對(duì)博客已經(jīng)足夠使用了,但是對(duì)于產(chǎn)品類型的文章來說,不僅僅需要標(biāo)題和正文,還需要單獨(dú)設(shè)置一些其它的參數(shù),如產(chǎn)品價(jià)格、產(chǎn)品型號(hào)、規(guī)格大小等,那么就需要給文章類型添加Meta Box,通俗點(diǎn)理解就是自定義字段表單,下面就來以實(shí)例講解下這個(gè)函數(shù)的用法。
語法結(jié)構(gòu)
<?php add_meta_box(
$id,
$title,
$callback,
$post_type,
$context,
$priority,
$callback_args
);
?>
參數(shù)
$id(字符串)(必需)字段id,唯一
$title(字符串)(必需)標(biāo)題名稱,顯示在文章編輯頁面
$callback(回調(diào))(必需)回調(diào)函數(shù)
$post_type(字符串)(必需)文章類型
$context(字符串)(可選)顯示位置,文章編輯頁面包括’normal’, ‘side’, and ‘advanced’的形式,Menus meta boxes僅用’side’的形式
$priority(字符串)(可選)優(yōu)先級(jí),默認(rèn)值: ‘default’
$callback_args(數(shù)組)(可選)傳遞到 callback 函數(shù)的參數(shù)。callback 函數(shù)將接收 $post 對(duì)象和其他由這個(gè)變量傳遞的任何參數(shù)。
實(shí)例
add_action( 'add_meta_boxes', 'product_price' );
function product_price() {
add_meta_box(
'product_price',
'產(chǎn)品價(jià)格',
'product_price_meta_box',
'store',
'side',
'low'
);
}
創(chuàng)建回調(diào)函數(shù)product_price_meta_box
配置參數(shù)里面指定了回調(diào)函數(shù)product_price_meta_box,需要在這個(gè)函數(shù)里面創(chuàng)建表單,
隱藏的自定義字段
插件/主題開發(fā)人員如果需要用自定義字段來保存插件或模板相關(guān)參數(shù),會(huì)發(fā)現(xiàn)WordPress不會(huì)在頁面/文章編輯頁的自定義字段列表上顯示以”_”(下劃線)開始的關(guān)鍵字。這樣就可以在自定義參數(shù)中將下劃線作為第一個(gè)字符,這些設(shè)置將按自定義字段被保留,但卻不會(huì)在管理者用戶界面的自定義字段中顯示出來。
function product_price_meta_box($post) {
// 創(chuàng)建臨時(shí)隱藏表單,為了安全
wp_nonce_field( 'product_price_meta_box', 'product_price_meta_box_nonce' );
// 獲取之前存儲(chǔ)的值
$value = get_post_meta( $post->ID, '_product_price', true );
?>
<label for="product_price"></label>
? ?<input style="width:180px" type="text" id="product_price" name="product_price" value="<?php echo esc_attr( $value ); ?>" placeholder="輸入產(chǎn)品價(jià)格">
? ?<span>價(jià)格</span>
<?php
}
提示:添加上面代碼后,新建文章時(shí),在右則就可以看到一個(gè)產(chǎn)品價(jià)格的輸入框。
這時(shí)候表單還不能用,因?yàn)樘峤晃恼轮蟛]有保存這個(gè) Meta Box 的內(nèi)容,下面是驗(yàn)證保存內(nèi)容的代碼:
add_action( 'save_post', 'product_price_save_meta_box' );
function product_price_save_meta_box($post_id){
if ( ! isset( $_POST['product_price_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['product_price_meta_box_nonce'], 'product_price_meta_box' ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! isset( $_POST['product_price'] ) ) {
return;
}
$product_price = sanitize_text_field( $_POST['product_price'] );
update_post_meta( $post_id, '_product_price', $product_price );
}
把上面的代碼按順序添加到主題的functions.php文件,至此,Meta Box注冊(cè)完成,就可以開始添加參數(shù)了:
調(diào)用代碼
<?php
if(get_post_meta($post->ID,'_product_price',true)){
echo get_post_meta($post->ID,'_product_price',true);
}
?>
把META BOX添加把后臺(tái)所有產(chǎn)品列表字段中顯示
通過manage_$post_type_posts_custom_column實(shí)現(xiàn),代碼如下
add_filter('manage_store_posts_columns', 'add_new_product_columns');
function add_new_product_columns($columns) {
$columns['id'] = 'ID';
$columns['product_price'] = '產(chǎn)品價(jià)格';
return $columns;
}
add_action('manage_store_posts_custom_column', 'manage_posts_columns', 10, 2);
function manage_posts_columns($column,$id) {
global $post;
switch ($column) {
case 'id':
echo $id;
break;
case 'product_price':
echo get_post_meta( $post->ID, '_product_price', true );
break;
}
}
1 CDN是什么?有什么用?
如何10倍提升外貿(mào)網(wǎng)站速度?從而提高谷歌搜索排名?
內(nèi)容分發(fā)網(wǎng)絡(luò)
”。在本文中,我們將分享可以幫助你加快外貿(mào)網(wǎng)站速度的WordPress最佳CDN服務(wù)。
多年來MaxCDN是個(gè)非常流行的CDN服務(wù),特別是對(duì)于WordPress用戶:
但是,Stackpath為你提供了很多選擇,你可以選擇特定服務(wù),或者使用包含CDN、防火墻、托管DNS、全球DDoS保護(hù)等的完全“邊緣交付包”。
Stackpath的全球DDoS保護(hù):
目前,Stackpath 在除非洲之外的每個(gè)宜居大陸上,提供了35個(gè)以上的CDN節(jié)點(diǎn)。 你可以查看以下地圖 ▼
點(diǎn)此進(jìn)入 Stackpath官網(wǎng) 查看最新全球CDN節(jié)點(diǎn)
Stackpath的優(yōu)點(diǎn)有哪些?
第1步:注冊(cè)StackPath CDN?賬號(hào)▼
點(diǎn)此進(jìn)入 StackPath CDN?官網(wǎng)
輸入郵箱和密碼,并單擊“Create an Account”按鈕,創(chuàng)建一個(gè)帳戶?▼
第 2 步:需要選擇一項(xiàng)StackPath服務(wù)。StackPath提供網(wǎng)站和應(yīng)用程序服務(wù)以及邊緣計(jì)算服務(wù)? 選擇一“網(wǎng)站和應(yīng)用程序服務(wù)”?▼
第 3 步:選擇StackPath的 CDN?▼
第 3 步:通過發(fā)送到你的電子郵件帳戶的鏈接驗(yàn)證你的電子郵件地址后,它會(huì)將你重定向到付款頁面 ▼
第 4 步:在StackPath儀表板中,單擊Site選項(xiàng)卡 ▼
第 5 步:創(chuàng)建StackPath CDN站點(diǎn) ▼
在大多數(shù)情況下,這是網(wǎng)站的URL。
bucket
.s3-?aws-region
.amazonaws.comaws-region
.amazonaws.com /bucket-name
第 6 步:將StackPath CDN URL粘貼到Autoptimize插件的CDN Base URL字段中?▼
http://
或 https://
才能使用Autoptimize 插件。第 7 步:在StackPath中轉(zhuǎn)到CDN→CACHE SETTINGS(緩存設(shè)置)▼
第 8 步:在StackPath中將你的服務(wù)器IP地址列入白名單(WAF→防火墻)?▼
在GTmetrix中測(cè)試運(yùn)行你的站點(diǎn) ,YSlow中的“內(nèi)容交付網(wǎng)絡(luò)”應(yīng)為綠色?▼
如果使用WordPress建站,可以安裝WordPress插件Autoptimize。
Google字體:
優(yōu)化圖片:
圖像優(yōu)化質(zhì)量:
刪除Emojis:
從靜態(tài)資源中刪除查詢字符串:
預(yù)連接到第3方域名:
https://fonts.googleapis.com https://fonts.gstatic.com https://www.google-analytics.com https://ajax.googleapis.com https://connect.facebook.net https://www.googletagmanager.com https://maps.google.com
異步Javascript文件:
優(yōu)化YouTube視頻:
到此,我們已經(jīng)完成了Autoptimize設(shè)置中對(duì)StackPath CDN的配置。
點(diǎn)此進(jìn)入 StackPath CDN 官網(wǎng)
希望我們網(wǎng)站( http://news.qtyiliao.cn/ ) 分享的《國(guó)外CDN服務(wù)商外貿(mào)免備案推薦:Stackpath CDN設(shè)置教程》,對(duì)您有幫助。
init 鉤子在大多數(shù)的 WordPress 程序都加載之后進(jìn)行加載。WordPress 同樣添加許多內(nèi)部的功能到這個(gè)鉤子中,例如 post types 和 taxonomies 以及默認(rèn) widgets 的初始化。
加載這個(gè)鉤子時(shí)幾乎 WordPress 中的所有內(nèi)容都就緒了,當(dāng) WordPress 的所有信息都可用時(shí),你的插件使用這個(gè)鉤子差不多可以做任何想做的事情了。
下面的例子中,為用戶添加了product post type形式
function my_custom_post_product() {
$args = array();
register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );
bloginfo() 直接在瀏覽器中輸出內(nèi)容,我們創(chuàng)建一個(gè)wordpress博客的時(shí)候,我們需要填寫博客的相關(guān)信息,包括博客名稱,博客描述,博客地址等等。當(dāng)我們需要使用這些信息的時(shí)候,就可以使用bloginfo()函數(shù)來獲取wordpress博客的相關(guān)信息。
語法結(jié)構(gòu)
<?php bloginfo( $show ); ?>
參數(shù):
$show (字符串string) (可選)你需要輸出的信息的關(guān)鍵詞。 默認(rèn)值: name
‘name’:顯示在 設(shè)置 -> 常規(guī) 中設(shè)置的“站點(diǎn)標(biāo)題”。該數(shù)據(jù)是從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的 “blogname”記錄。
‘description’:顯示在 設(shè)置 -> 常規(guī) 中設(shè)置的“副標(biāo)題”。該數(shù)據(jù)是從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的 “blogdescription” 記錄。
‘admin_email’:顯示在 設(shè)置 > 常規(guī) 中設(shè)置的 “電子郵件地址”。該數(shù)據(jù)是從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的 “admin_email”記錄。
‘charset’:顯示在 設(shè)置 > 常規(guī) 中設(shè)置的“頁面和feed的編碼”。該數(shù)據(jù)是從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的”blog_charset” 記錄。(注:3.5.1+好像已經(jīng)沒有這個(gè)選項(xiàng)了)
‘html_type’:顯示W(wǎng)ordPress HTML 頁面中的內(nèi)容類型(默認(rèn): “text/html”)。該數(shù)據(jù)可以從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的 “html_type” 記錄。主題和插件可以通過使用 pre_option_html_type 過濾器覆蓋默認(rèn)值。
‘language’:顯示W(wǎng)ordPress的語言。
‘wpurl’:顯示在 設(shè)置 > 常規(guī) 中設(shè)置的 “WordPress 地址 (URL)”。該數(shù)據(jù)是從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的 “siteurl” 記錄。 可以考慮使用 site_url() 來代替,尤其是在使用 子目錄路徑方式,而不是使用 子域名 來配置多站點(diǎn)時(shí)(bloginfo將返回根網(wǎng)站的URL,而不是子站點(diǎn)的URL)。
‘url’:顯示在 設(shè)置 > 常規(guī) 中設(shè)置的 “站點(diǎn)地址(URL)”。該數(shù)據(jù)是從 wp_options 這個(gè)數(shù)據(jù)表中檢索到的 “home”記錄。 可以考慮使用 home_url() 代替。
‘stylesheet_url’:顯示當(dāng)前使用的主題的 CSS文件(通常為 style.css)路徑。可以考慮使用 get_stylesheet_uri() 代替。
‘template_url’/’template_directory’:當(dāng)前主題的 URL 路徑 。在子主題中, get_bloginfo(‘template_url’) 和 get_template() 都將返回父主題的目錄??梢钥紤]使用 get_template_directory_uri() (用于父主題目錄)或get_stylesheet_directory_uri() (用于子主題目錄)代替。
‘stylesheet_directory’:顯示當(dāng)前使用的主題的樣式表路徑??梢钥紤]使用 get_stylesheet_directory_uri() 代替。
示例輸出
name = 獲得更好的筆記查詢體驗(yàn)
description = 又一個(gè)WordPress站點(diǎn)
admin_email = admin@example.com
charset = UTF-8
html_type = text/html
language = en-US
wpurl = http://coding.xuxiaoke.com/(獲得安裝路徑)
url = http://coding.xuxiaoke.com/(獲得首頁地址)
stylesheet_url = http://www.example.com/home/wp/wp-content/themes/bluesky/style.css
stylesheet_directory = http://www.example.com/home/wp/wp-content/themes/bluesky
template_directory = http://www.example.com/home/wp/wp-content/themes/bluesky
template_url = http://www.example.com/home/wp/wp-content/themes/bluesky
text_direction = ltr
version = 3.5
home = http://www.example.com/home (已棄用!使用 url 替代)
siteurl = http://www.example.com/home (已棄用!使用 url 替代)
add_settings_section()函數(shù)的作用主要是為WordPress后臺(tái)-Settings里面的某個(gè)欄目添加一個(gè)能夠?qū)崿F(xiàn)add_settings_field( )函數(shù)添加自定義變量的區(qū)域,與add_settings_field( )函數(shù)配合使用可以實(shí)現(xiàn)Settings里面的某個(gè)欄目(’general’, ‘reading’, ‘writing’, ‘discussion’, ‘media)添加自定義變量的功能。
用法
<?php
add_settings_section(
string?$id,
string?$title,
callable?$callback,
string?$page?
) ;
?>
例子:
<?php
add_settings_section(
'xk_settings_section', // 此處自己命名,用于標(biāo)簽的ID屬性
'聯(lián)系方式', // 顯示在頁面的標(biāo)題
'xk_settings_section_callback', // 頁面回掉
'general' // 設(shè)置里面的欄目包括:'general', 'reading', 'writing', 'discussion', 'media'
);
?>
相關(guān)函數(shù):add_settings_field( )函數(shù)
register_setting()函數(shù)
實(shí)戰(zhàn)案例可在register_setting()函數(shù)查看