【问题标题】:Adding "Home" at the beginning of breadcrumbs在面包屑开头添加“家”
【发布时间】:2020-04-29 03:19:24
【问题描述】:

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

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;

}

提前感谢您的帮助!

【问题讨论】:

  • 为什么不总是在开始时将 Home 打印为“硬编码”?在面包屑的开头有 home 不是一件小事吗?
  • 我想从一开始就对其进行硬编码,但我不知道 php。添加它的代码是什么?
  • 在下面查看我的解决方案

标签: php wordpress woocommerce styles breadcrumbs


【解决方案1】:

我认为您可以通过将以下行 echo $wrap_before; 更改为:

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

【讨论】:

  • 是的,这解决了它!非常感谢,感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多