繁体   English   中英

在面包屑的开头添加“主页”

[英]Adding "Home" at the beginning of breadcrumbs

我在我的临时站点上格式化面包屑时遇到问题。 我之前让它们设置了正确的类别,但父类别一直显示错误的类别(我确实修复了,但是......)。 该修复导致“主页”选项消失。 我不知道任何 php 并且一直在尝试弄清楚如何在每个页面上的类别之前添加一个“主页/”选项,但没有成功。 下面是我现在使用的代码。 谁有想法?

if ( ! empty( $breadcrumb ) ) {

    echo $wrap_before;

    if ( is_single() && get_post_type() == 'product' ) {

        echo $prepend;

        if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {

            $referer = wp_get_referer();

            $printed = array();

            foreach( $terms as $term){

                if(in_array($term->id, $printed)) continue;

                $referer_slug = (strpos($referer, '/'.$term->slug.'/'));

                if(!$referer_slug==false){

                    $printed[] = $term->id;

                    $category_name = $term->name;
                    $ancestors = get_ancestors( $term->term_id, 'product_cat' );
                    $ancestors = array_reverse( $ancestors );

                    foreach ( $ancestors as $ancestor ) {
                        $ancestor = get_term( $ancestor, 'product_cat' );

                        if ( ! is_wp_error( $ancestor ) && $ancestor )
                            echo $before . '<a href="' . get_term_link( $ancestor->slug, 'product_cat' ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
                    }

                    echo $before . '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $category_name . '</a>' . $after . $delimiter;
                }
            }

        }

        echo $before . get_the_title() . $after;

    } else {

        foreach ( $breadcrumb as $key => $crumb ) {

            echo $before;

            if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
                echo '<a href="' . esc_url( $crumb[1] ) . '">' . esc_html( $crumb[0] ) . '</a>';
            } else {
                echo esc_html( $crumb[0] );
            }

            echo $after;

            if ( sizeof( $breadcrumb ) !== $key + 1 ) {
                echo $delimiter;
            }

        }
    }

    echo $wrap_after;

}

我在这里先向您的帮助表示感谢!

我认为您可以通过更改以下行来实现这一点echo $wrap_before; 对此:

echo $wrap_before.'<a href="/" title="Home">Home</a> »';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM