head头部有很多代码都和seo有关,比如可以添加规范标签canonical、robots.txt标签等等。那么如何自定义在分类中添加head代码呢
代码
- //自定义添加head代码
- global $texonomy_slug_definedhead;
- $texonomy_slug_definedhead='category';
- add_action($texonomy_slug_definedhead.'_add_form_fields','categorydefinedhead');
- function categorydefinedhead($taxonomy){ ?>
- <div>
- <label for="tag-definedhead">自定义head</label>
- <input type="text" name="tag-definedhead" id="tag-definedhead" value="" /><br /><span>输入head代码</span>
- </div>
- <?php }
- add_action($texonomy_slug_definedhead.'_edit_form_fields','categorydefinedheadedit');
- function categorydefinedheadedit($taxonomy){ ?>
- <tr class="form-field">
- <th scope="row" valign="top"><label for="tag-definedhead">自定义head</label></th>
- <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>
- </tr>
- <?php }
- add_action('edit_term','categorydefinedheadsave');
- add_action('create_term','categorydefinedheadsave');
- function categorydefinedheadsave($term_id){
- if(isset($_POST['tag-definedhead'])){
- if(isset($_POST['tag-definedhead']))
- update_option('_category_definedhead'.$term_id,$_POST['tag-definedhead'] );
- }
- }
在函数模板上面的代码,添加一个字段
调用
- //
- 分类添加自定义head
- <?php
- if(is_category()){
- $title = the_zm_title();
- $category_info = get_the_category();
- $category_id = $category_info[0]->cat_ID;
- $cat_definedhead = get_option('_category_definedhead'.$category_id);
- ?>
- <?php echo $cat_definedhead;?>
- <?php } ?>
将代码放到header中使用即可
转载请注明:思享SEO博客 » wordpress分类目录添加自定义head内容
扩展阅读
WordPress分类和标签页URL结尾添加/斜杠
WordPress 固定链接 自定义结构以.html结尾时,分类和标签页的链接尾部将不会以/结尾,这样的链接一般是无法被缓存的。 两种纯代码添加斜杠的方法 任选一种代码,复制到所用主题 functions.php 底部保存生效 方法一 //给标签和分......
wordpress后台编辑器添加自定义标签按钮
增强wordpress编辑器是必要的,因为有的只是一些基础的按钮,比如我想要使用pre标签的按钮就没有 function tt_add_quicktags() { ?> <script type="text/javascript">......
WordPress怎么获取当前页面的URL
在WordPress主题和插件的开发过程中,经常需要获取当前页面的URL,之前我也写了个教程,讲解如何使用PHP获取当前页面的URL,不过使用php获取访问URL受网站环境影响较大,代码过多。我就在想,能不能使用WordPress原生的函数来实现这个......