2

I am using Craft only for the blog section of my HTML site. I have integrated my existing breadcrumbs to the blog, but there is a problem.

When I am on the home page of the blog, the word "Blog" in the breadcrumb is linked, and I would rather it not be linked.

I would like the word "Blog" in the breadcrumb to be a link only after going to an article on the blog. Please see attached.

Screen shot comparing the Home page of the blog and an Article page

The code I am using right now is

    <ol class="breadcrumb">
        <li><a href="/index.html">Home</a></li>
            <li><a href="{{ siteUrl }}">Blog</a></li>
            {% for crumb in entry.getAncestors() %}
                <li>{{ crumb.getLink() }}</li>
            {% endfor %}
            <li>{{ entry.title() }}</li>
    </ol>

I am new to Craft - thanks for your help!

Steve VH
  • 31
  • 3

2 Answers2

2

A conditional that keys off the presence of the second segment to determine what to show for the "Blog" link/copy will work.

{% set seg2 = "" %}
{% set seg2 = craft.request.getSegment(2) %}

<ol class="breadcrumb">
    <li><a href="/">Home</a></li>
    <li>
        {% if seg2 == "" %}
            Blog
        {% else %}
            <a href="{{ siteUrl }}">Blog</a>
        {% endif %}
    </li>
    {% for crumb in entry.getAncestors() %}
        <li>{{ crumb.getLink() }}</li>
    {% endfor %}
    <li>{{ entry.title() }}</li>
</ol>
Anna_MediaGirl
  • 2,503
  • 2
  • 17
  • 45
  • Worked like a charm ... I am still getting a "/" after "Blog" on the Home page of the Blog but I can live with that. TY again. – Steve VH Mar 26 '15 at 22:27
  • Is the "/" coming from your CSS? It's not in the code that you posted. – Anna_MediaGirl Mar 26 '15 at 22:29
  • I think it's a bootstrap style. If you 'inspect element' ::before is shown and I think that is where the slashes are coming from. – Steve VH Mar 27 '15 at 15:20
  • @SteveVH If so, you can wrap the "Blog" text within the conditional in a span, e.g. <span class="nolink">Blog</span>, and hide the slash in your CSS like so: .nolink:before { display: none; } – Mats Mikkel Rummelhoff Mar 28 '15 at 20:58
2

First, if your Blog section type is a Channel and not a Structure, then {% for crumb in entry.getAncestors() %} won't return anything. Your entry needs to be in a structure have any ancestors.

Here's a rundown of Craft's section types:

http://buildwithcraft.com/docs/sections-and-entries

This is really going to depend on how you have your templates and sections are setup, but here's how a basic setup for a Blog channel might work.

Let's say you have a Blog section that's a Channel section type. In your craft/templates folder, you have the following folders:

blog/
blog/index.html
blog/_entry.html

In the settings for the Blog section, you have...

Entry URL Format: blog/{slug}

Entry Template: blog/_entry

If you're on an entry page like example.com/blog/what-makes-a-good-logo-design, Craft will route to the blog/_entry.html template. In that file your breadcrumb could be just this:

<ol class="breadcrumb">
    <li><a href="{{ siteUrl }}">Home</a></li>
    <li><a href="/blog">Blog</a></li>
    <li>{{ entry.title }}</li>
</ol>

If you go to example.com/blog, Craft will route to the blog/index.html template. In that file, you can have a separate breadcrumb like this:

<ol class="breadcrumb">
    <li><a href="{{ siteUrl }}">Home</a></li>
    <li>Blog</li>
</ol>
Alex Roper
  • 2,632
  • 1
  • 11
  • 23
  • Thanks for the further clarification. I'm new to Craft and still trying to understand all of it! – Steve VH Mar 27 '15 at 15:34
  • OK so I just had that "aha" moment - I really didn't understand until now about the "block - endblock" concept and how you can replace different sections with new stuff.

    I mentioned to Craft how they really need to have a primer for the non-developer/designer that gives a step-by-step on how to develop a basic Craft site, like Mike Boyink's and Ryan Irelan's [excellent] books for EE. :)

    – Steve VH Mar 27 '15 at 16:15
  • There's a good list of beginner resources on this question "Are there any beginner Craft tutorials?": http://craftcms.stackexchange.com/a/1826/273 – Alex Roper Mar 27 '15 at 22:49