duancongjue9202 2017-01-05 17:35
浏览 78
已采纳

WordPress - 引导父母和子类别面包屑

I have a php function to display breadcrumbs, however it display wrong order

for strong text example

Home / Child/ Parent / post

when it should be

Home / Parent / Child / Posts

Why?

Function

function bavota_breadcrumbs() {
    if(!is_home()) {
        echo '<nav class="breadcrumb">';
        echo '<a href="'.home_url('/').'">'.get_bloginfo('name').'</a><span class="divider">/</span>';
        if (is_category() || is_single()) {
            the_category(' <span class="divider">/</span> ');
            if (is_single()) {
                echo ' <span class="divider">/</span> ';
                the_title();
            }
        } elseif (is_page()) {
            echo the_title();
        }
        echo '</nav>';
    }
}
  • 写回答

1条回答 默认 最新

  • dreinuqm992401 2017-01-06 09:13
    关注

    Can you please try below code. do not use the_category display all category in which post assign to it. below function is show only it's parent category.

    function bavota_breadcrumbs() {
        if(!is_home()) {
            echo '<nav class="breadcrumb">';
            echo '<a href="'.home_url('/').'">'.get_bloginfo('name').'</a><span class="divider">/</span>';
            if (is_category() || is_single()) {
                //the_category(' <span class="divider">/</span> ');
                global $wp_query;
                $object = $wp_query->get_queried_object();
    
                // Get parents of current category
                $parent_id  = $object->category_parent;
                $cat_breadcrumbs = '';
                while ($parent_id) {
                    $category   = get_category($parent_id);
                    echo '<a href="' . get_category_link($category->cat_ID) . '">' . $category->cat_name . '</a>';
                    $parent_id  = $category->category_parent;
                    echo ' <span class="divider">/</span> ';
                }
                echo '<span>'.$object->cat_name.'</span>';
    
                if (is_single()) {
                    echo ' <span class="divider">/</span> ';
                    the_title();
                }
            } elseif (is_page()) {
                echo the_title();
            }
            echo '</nav>';
        }
    }
    

    see the_category() function here

    This code is tested and work perfect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”