有时候,我们需要通过分类的序号来排序分类列表,wordpress的分类有个term_order属性,默认为0。
<?php function mbt_add_category_field(){ echo '<div class="form-field"> <label for="cat-num">序号</label> <input name="_term_order" id="cat-num" type="text" value="" size="40"> <p>专题列表排序,数字越好,越靠前</p> </div>'; } add_action('category_add_form_fields','mbt_add_category_field',10,2); // 分类编辑字段 function mbt_edit_category_field($tag){ echo '<tr class="form-field"> <th scope="row"><label for="cat-num">排序</label></th> <td> <input name="_term_order" id="cat-num" type="text" value="'; echo ( ! empty( $tag->term_group ) ) ? $tag->term_group : '0'; echo '" size="40"/><br> <span class="cat-num">'.$tag->name.' 的专题排序</span> </td> </tr>'; } add_action('category_edit_form_fields','mbt_edit_category_field',10,2); // 保存数据 function mbt_taxonomy_metadate($term_id){ global $wpdb; if( isset( $_POST['_term_order'] ) ) {$wpdb->update( $wpdb->terms,array('term_group' => $_POST['_term_order']),array( 'term_id'=> $term_id));} } // 虽然要两个钩子,但是我们可以两个钩子使用同一个函数 add_action('created_category','mbt_taxonomy_metadate',10,1); add_action('edited_category','mbt_taxonomy_metadate',10,1); ?>
使用方法:
<?php $catid = $options['topiccatid']; $args=array( 'child_of'=>$catid, 'orderby' => 'term_group', 'order'=>'ASC', 'hide_empty' => 0, ); $categories=get_categories($args); ?>
原文链接:https://www.dqzy.cn/2025/05/20/676.html,转载请注明出处。
1、本站所有源码资源(包括源代码、软件、学习资料等)仅供研究学习以及参考等合法使用,请勿用于商业用途以及违法使用。如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,请用户在下载使用前必须详细阅读并遵守软件作者的“使用许可协议”,本站仅仅是一个学习交流的平台。
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为:www.dqzy.cn。
4、点启资源网是一个免费且专业分享网站源码、图片素材、特效代码、教程文章、站长工具的平台。我们努力给站长提供好的资源!
评论0