在wordpress企業(yè)主題開發(fā)中wordpress分類欄目通常是需要單獨設置縮略圖的,之前給大家分享過wordpress分類縮略圖插件,今天給大家分享一個如何通過代碼來添加分類欄目縮略圖字段功能
將如下代碼添加到wordpress主題functions.php中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//////////////////////////////////為分類添加縮略圖 function salong_add_category_field(){ echo '<div class="form-field"> <label for="thumb">'.__('縮略圖','salong').'</label> <input name="thumb" id="thumb" type="text" value="" size="40"> <p>'.__('輸入分類的縮略圖鏈接。','salong').'</p> </div>'; } add_action('category_add_form_fields','salong_add_category_field',10,2); // 分類編輯字段 function salong_edit_category_field($tag){ echo '<tr class="form-field"> <th scope="row"><label for="thumb">'.__('灰色地圖','salong').'</label></th> <td> <input name="thumb" id="thumb" type="text" value="'; echo get_option('thumb-'.$tag->term_id).'" size="40"/><br> <span class="thumb">'.$tag->name.__('分類的縮略圖鏈接。','salong').'</span> </td> </tr>'; } add_action('category_edit_form_fields','salong_edit_category_field',10,2); // 保存數(shù)據(jù) function salong_category_thumb($term_id){ if(isset($_POST['thumb'])){ //判斷權限--可改 if(!current_user_can('manage_categories')){ return $term_id; } $thumb_key = 'thumb-'.$term_id; $thumb_value = $_POST['thumb']; // 更新選項值 update_option( $thumb_key, $thumb_value ); } } // 雖然要兩個鉤子,但是我們可以兩個鉤子使用同一個函數(shù) add_action('created_category','salong_category_thumb',10,1); add_action('edited_category','salong_category_thumb',10,1); |
添加好以上代碼,如何調(diào)用呢,在需要顯示分類縮略圖的位置添加以下代碼即可完成自動的調(diào)用功能。
1 |
echo get_option('thumb_color-'.$category_id) |