wordpress分类目录添加自定义head内容

自媒体 思享 1801浏览

head头部有很多代码都和seo有关,比如可以添加规范标签canonical、robots.txt标签等等。那么如何自定义在分类中添加head代码呢

wordpress

代码

  1. //自定义添加head代码
  2. global $texonomy_slug_definedhead;
  3. $texonomy_slug_definedhead='category';
  4. add_action($texonomy_slug_definedhead.'_add_form_fields','categorydefinedhead');
  5. function categorydefinedhead($taxonomy){ ?>
  6. <div>
  7. <label for="tag-definedhead">自定义head</label>
  8. <input type="text" name="tag-definedhead" id="tag-definedhead" value="" /><br /><span>输入head代码</span>
  9. </div>
  10. <?php }
  11. add_action($texonomy_slug_definedhead.'_edit_form_fields','categorydefinedheadedit');
  12. function categorydefinedheadedit($taxonomy){ ?>
  13. <tr class="form-field">
  14. <th scope="row" valign="top"><label for="tag-definedhead">自定义head</label></th>
  15. <td><input type="text" name="tag-definedhead" id="tag-definedhead" value="<?php echo get_option('_category_definedhead'.$taxonomy->term_id); ?>" /><br /><span class="description">输入head代码</span></td>
  16. </tr>
  17. <?php }
  18. add_action('edit_term','categorydefinedheadsave');
  19. add_action('create_term','categorydefinedheadsave');
  20. function categorydefinedheadsave($term_id){
  21. if(isset($_POST['tag-definedhead'])){
  22. if(isset($_POST['tag-definedhead']))
  23. update_option('_category_definedhead'.$term_id,$_POST['tag-definedhead'] );
  24. }
  25. }

在函数模板上面的代码,添加一个字段

调用

  1. //
  2. 分类添加自定义head
  3. <?php
  4. if(is_category()){
  5. $title = the_zm_title();
  6. $category_info = get_the_category();
  7. $category_id = $category_info[0]->cat_ID;
  8. $cat_definedhead = get_option('_category_definedhead'.$category_id);
  9. ?>
  10. <?php echo $cat_definedhead;?>
  11. <?php } ?>

将代码放到header中使用即可

推荐阅读

纯代码实现wordpress附件页面重定向到文章或首页

前几天发现wordpress网站有评论留言的回顾。结果一看就是附件页面的垃圾评论。这才发现原来wordpress上传的附件也会有对应的页面。难怪之前收录了很多附件页面,但是我的机器人禁止了,忘记了。本来以为会禁用,结果只找到了在线使用插件的方法。最后......

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘fake_update_callback’

这是当一个被挂钩的函数名与挂钩关联不匹配时…这可能发生在重命名一个函数时,而不是在挂钩关联中重命名函数名时。如果强迫症受不了wordpress的主题提示,可以使用插件WP降级将版本设置为当前版本。...

wordpress升降到指定版本:WP Downgrade | Specific Core Version插件

背景:网站一直用4.9+版本,因为编辑器还是老的。如果是自动升级,是目前最高的版本,但是我想升级到最新的版本,4.9.22版。WP降级|特定核心版插件可以解决我的问题。设置好程序路径后,记得保存更改,然后“升/降级核心”会等待升级。如果是降级操作,为......