WordPress分类栏目添加缩略图封面功能

我们通常在wordpress企业主题开发中wordpress分类栏目通常是需要单独设置缩略图的,今天给大家分享一个如何通过代码来添加分类栏目缩略图字段功能,将如下代码添加到wordpress主functions.php中:

  1. function salong_add_category_field(){
  2. echo '<div class="form-field">
  3. <label for="thumb">'.__('缩略图','salong').'</label>
  4. <input name="thumb" id="thumb" type="text" value="" size="40">
  5. <p>'.__('输入分类的缩略图链接。','salong').'</p>
  6. </div>';
  7. }
  8. add_action('category_add_form_fields','salong_add_category_field',10,2);
  9. // 分类编辑字段
  10. function salong_edit_category_field($tag){
  11. echo '<tr class="form-field">
  12. <th scope="row"><label for="thumb">'.__('灰色地图','salong').'</label></th>
  13. <td>
  14. <input name="thumb" id="thumb" type="text" value="';
  15. echo get_option('thumb-'.$tag->term_id).'" size="40"/><br>
  16. <span class="thumb">'.$tag->name.__('分类的缩略图链接。','salong').'</span>
  17. </td>
  18. </tr>';
  19. }
  20. add_action('category_edit_form_fields','salong_edit_category_field',10,2);
  21. // 保存数据
  22. function salong_category_thumb($term_id){
  23. if(isset($_POST['thumb'])){
  24. //判断权限--可改
  25. if(!current_user_can('manage_categories')){
  26. return $term_id;
  27. }
  28. $thumb_key = 'thumb-'.$term_id;
  29. $thumb_value = $_POST['thumb'];
  30. // 更新选项值
  31. update_option( $thumb_key, $thumb_value );
  32. }
  33. }
  34. // 虽然要两个钩子,但是我们可以两个钩子使用同一个函数
  35. add_action('created_category','salong_category_thumb',10,1);
  36. add_action('edited_category','salong_category_thumb',10,1);

添加好以上代码,如何调用呢,在需要显示分类缩略图的位置添加以下代码即可完成自动的调用功能:

  1. echo get_option('thumb_color-'.$category_id)
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/842
0 评论
3.2k

发表评论

!