wordpress教程:面包屑导航(更细版本)

作者 : 轩寒 本文共1836个字,预计阅读时间需要5分钟 发布时间: 2022-04-30 共207人阅读

之前站哈源码网分享过一例 wordpress面包屑 的代码教程,详情可见:GO  今天再分享一款更加细化的 wordpress面包屑导航,教程依然很简单,往functions.php加代码,然后前端调用即可,至于样式,自由发挥吧。代码如下:

breadcrumb
breadcrumb
function wpmomo_breadcrumb_html($post_id,$separator){
 $path[] = wpmomo_breadcrumb_output( home_url('/'), '首页');
 if( get_post_type($post_id)=='post' ) {
 $cats_id = array();
 $categories = get_the_category($post_id);
 if($categories){
 foreach($categories as $category) {
 if(!in_array($category->term_id,$cats_id)){
 if ( $category->parent ){
 $path[] = wpmomo_get_category_parents( $category->parent, $separator );
 $cats_id[] = $category->parent;
 }
 $path[] = wpmomo_breadcrumb_output( get_category_link( $category->term_id ), $category->name);
 $cats_id[] = $category->term_id;
 }
 }
 }
 }
 if( is_singular() && !is_single() && !is_page() ){
 $post_type = get_post_type();
 $post_type_obj = get_post_type_object( $post_type );
 $path[] = wpmomo_breadcrumb_output( get_post_type_archive_link( $post_type ), $post_type_obj->labels->singular_name);
 }
 $path[] = wpmomo_breadcrumb_output( get_permalink($post_id), get_the_title($post_id));
 echo join( $separator ,$path);
 }
 function wpmomo_get_category_parents( $id, $separator='', $visited = array() ) {
 $chain = '';
 $parent = get_term( $id, 'category' );
 if ( is_wp_error( $parent ) )
 return $parent;
 $name = $parent->name;
 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
 $visited[] = $parent->parent;
 $chain .= wpmomo_get_category_parents( $parent->parent, $separator, $visited );
 }
 $chain .= wpmomo_breadcrumb_output( get_category_link( $parent->term_id ), $name);
 return $chain;
 }
function wpmomo_breadcrumb_output($url,$name){
 return '<span">'.$name.'';
 }

代码放进去后,下面就是在你的前端位置加入调用了,代码如下:

<?php wpmomo_breadcrumb_html(get_the_ID(),'&nbsp› &nbsp'); ?>
版权免责声明

① 本站VIP源码资源永久免费下载 www.wayu.cn;
② 本网站资源来源于网络收集,如有侵权,请联系站长进行删除处理;
③ 分享目的仅供大家学习和交流,请不要用于商业用途,否则后果自负;
④ 如果你有源码需要出售,可以联系小编,或者点击 投稿;
⑤ 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解;
⑥ 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需;
⑦ 在您的能力范围内,为了大环境的良性发展,请尽可能的选择正版资源;
⑧ 网站资源不做任何二次加密和授权后门(原版加密除外)。


站哈源码网 » wordpress教程:面包屑导航(更细版本)

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

发表评论

Copyright© 2022 by-站哈源码网 & 站内部分资源收集于网络,若侵犯了您的合法权益,请联系站长删除!
在 1.112 秒钟有 41 次查询。
开通VIP 享更多特权,建议使用QQ登录