中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

給 wordpress分類目錄添加自定義META屬性字段

2018-11-01    來源:學(xué)做網(wǎng)站論壇

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

很多使用WORDPRESS程序建網(wǎng)站的同學(xué)都苦于WP程序的分類目錄的屬性太少了,只有分類的標(biāo)題,別名,描述。不能像文章模板里可以調(diào)用wordpress自定義欄目,而分類目錄下的其它屬性卻很難自定義。

給 wordpress分類目錄添加自定義META屬性字段

為了讓我們更好的利用wordpress程序做出需要的網(wǎng)站功能效果,學(xué)做網(wǎng)站論壇分享一下如何給分類目錄添加自定義的META屬性。

1. ?在模板目錄下的 function.php 里,插入以下代碼,用于添加包含 頁面 category_field.php

// 分類添加字段
require_once( dirname(__FILE__).‘/category_field.php’ );

2. ?新建 category_field.php 頁面,代碼如下:

<?php
// 分類添加字段
function ems_add_category_field(){
echo ‘<div class=“form-field”>
<label for=“cat-tel”>Tel</label>
<input name=“cat-tel” id=“cat-tel” type=“text” value=“” size=“40”>
<p>The telephone.</p>
</div>';
echo ‘<div class=“form-field”>
<label for=“cat-url”>URL</label>
<input name=“cat-url” id=“cat-url” type=“text” value=“” size=“40”>
<p>The URL.</p>
</div>';
}
add_action(‘category_add_form_fields’,’ems_add_category_field’,10,2);
// 分類編輯字段
function ems_edit_category_field($tag){
echo ‘<tr class=“form-field”>
<th scope=“row”><label for=“cat-tel”>Tel</label></th>
<td>
<input name=“cat-tel” id=“cat-tel” type=“text” value=”‘;
echo get_option(‘cat-tel-‘.$tag->term_id).’” size=”40″/><br>
<span class=“cat-tel”>‘.$tag->name.’ on the phone.</span>
</td>
</tr>';
echo ‘<tr class=“form-field”>
<th scope=“row”><label for=“cat-url”>URL</label></th>
<td>
<input name=“cat-url” id=“cat-url” type=“text” value=”‘;
echo get_option(‘cat-url-‘.$tag->term_id).’” size=”40″/><br>
<span class=“cat-url”>‘.$tag->name.’ on the URL.</span>
</td>
</tr>';
}
add_action(‘category_edit_form_fields’,’ems_edit_category_field’,10,2);
// 保存數(shù)據(jù)
function ems_taxonomy_metadate($term_id){
if(isset($_POST[‘cat-tel’]) && isset($_POST[‘cat-url’])){
//判斷權(quán)限–可改
if(!current_user_can(‘manage_categories’)){
return $term_id;
}
// 電話
$tel_key = ‘cat-tel-‘.$term_id; // key 選項(xiàng)名為 cat-tel-1 類型
$tel_value = $_POST[‘cat-tel’]; // value
// url
$url_key = ‘cat-url-‘.$term_id;
$url_value = $_POST[‘cat-url’];
// 更新選項(xiàng)值
update_option( $tel_key, $tel_value );
update_option( $url_key, $url_value );
}
}
// 雖然要兩個(gè)鉤子,但是我們可以兩個(gè)鉤子使用同一個(gè)函數(shù)
add_action(‘created_category’,’ems_taxonomy_metadate’,10,1);
add_action(‘edited_category’,’ems_taxonomy_metadate’,10,1);
?>

3、調(diào)用方法(可以在wordpress文章模板(single.php)調(diào)用分類的META信息)

<?php
// 取出當(dāng)前分類 id: $categories[0]->term_id
$categories = get_the_category();
$term_id = $categories[0]->term_id;
$cat_name = $categories[0]->name;
?>
<div class=“phone”>
<div class=“p1″><?php echo get_option(‘cat-tel-‘.$term_id);?></div>
<div class=“p2″><a href=“<?php echo get_option(‘cat-url-‘.$term_id);?> “ target=“_blank”><?php echo $cat_name;?></a></div>
</div>

以上是通過代碼的方式給我們的分類目錄添加META屬性字段,我們還可以使用插件的方法來完成同樣的效果,方法見:https://www.xuewangzhan.com/wpbbs/6889.html

標(biāo)簽: 代碼 建網(wǎng)站 權(quán)限

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:如何解決wordpress后臺(tái)地址修改后無法登錄后臺(tái)

下一篇:不懂英文可以學(xué)習(xí)做網(wǎng)站嗎