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