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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀