Stop breadcrumbs script from removing duplicated text in URL paths

Hi,

This is my first post so apologies for any oversights or mistakes.

I have a breadcrumbs partial which I’ve modified from here Implementing breadcrumb navigation in hugo? - #57 by xforeal_2019, which works as expected for most pages.

The site stores content for different markets in directories, so for instance the UK market content is stored in /uk/ and the US market content /us/

Inside of each market there are continents, countries and locations, which for a handful of pages might create the following path
/us/north-america/us/new-york/

Currently when the market folder is the same as the country, i.e. /us/ appearing twice as above, the script removes /us/ from both the the country, so the URL for the New York page would be /us/north-america/new-york/ (as opposed to /us/north-america/us/new-york/).

Please can someone help with how to avoid this.

I’ve tried splitting the .URL by “/” and showing what it prints, which looks to be fine when I put it on the page.

Thanks in advance for the help.

<nav aria-label="breadcrumb" class="breadcrumb-nav">
  <div class="container">
    <ol class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">
      <li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
        {{ $marketFromPath := substr  .URL  0 4  }}

        {{ if in $marketFromPath "uk" }}
        <a href="{{  .Site.BaseURL  }}uk/" itemprop="item" class="link">
          <span itemprop="name" class="name">Home</span>
          <span class="chevron">></span>
          <meta itemprop="position" content="1" />
        </a>
        {{ end }}

        {{ if in $marketFromPath "us" }}
        <a href="{{  .Site.BaseURL  }}us/" itemprop="item" class="link">
          <span itemprop="name" class="name">Home</span>
          <span class="chevron">></span>
          <meta itemprop="position" content="1" />
        </a>
        {{ end }}
      </li>

      {{ $findUrl := slice }}
      
      {{ $el := ( split .URL "/" ) }}

      {{ range $el }}

      {{ if ge (len . ) 1 }}

      {{ $findUrl =  $findUrl | append . }}

      {{ end }}

      {{ end }}
      {{ $url := "/" }}

      {{ range $index, $el := $findUrl }}

        {{ if lt ( len $findUrl ) ( len . ) }}
        <li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
          {{ $url = "/" | add . | add $url  }}
          <a href="{{ substr $marketFromPath 0 3 }}{{ $url }}" itemprop="item" class="link">
            <span itemprop="name" class="name"> {{ (replace . "-" " ") }} </span>
            <span class="chevron">> </span>
            {{ $index = $index | add 1 }}
            <meta itemprop="position" content="{{ $index }}" />
          </a>
        </li>
        {{ end }}

      {{ end }}
    </ol>
  </div>
</nav>

       {{ range $index, $el := $findUrl }}
 
-        {{ if lt ( len $findUrl ) ( len . ) }}
         <li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
           {{ $url = "/" | add . | add $url  }}
-          <a href="{{ substr $marketFromPath 0 3 }}{{ $url }}" itemprop="item" class="link">
+          <a href="{{ $url }}" itemprop="item" class="link">
             <span itemprop="name" class="name"> {{ (replace . "-" " ") }} </span>
             <span class="chevron">> </span>
             {{ $index = $index | add 1 }}
             <meta itemprop="position" content="{{ $index }}" />
           </a>
         </li>
-        {{ end }}
+
 
       {{ end }}

Thank you so much for your help. I’m not really sure how I’ve managed to get it so mixed up in my head but I’m glad it was an easy fix.

When troubleshooting, I find it easiest to remove all the extraneous markup, and just look at the template code.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.