3

I have a Structure setup where I'd like each child to link to an anchor within the parent page (see the attached image).

enter image description here

But when I use <a href="{{ subEntry.url }}"> and click on the link, it redirects to the parent URL without the # anchor.

e.g. http://localhost:3000/services/web-interactive-design#website-design redirects to http://localhost:3000/services/website-design

How can I prevent this from happening?

3

The setup you have there is creating routes within the system not overriding the URL property of your entry. If you just want to generate anchors you should uncheck "Entries in this section have their own URLs"

Then within your template for your top-level structure item run a loop and output the content with ids and then the anchors as links.

{% set entries = craft.entries.section('myStructure') %}

<ul>
{% for entry in entries %}
    <li><a href="#{{ entry.slug }}">{{ entry.title }}</a></li>
{% endfor %}
</ul>

{% for entry in entries %}

    <div id="{{ entry.slug }}">
        {{ entry.content }}
    </div>

{% endfor %}
  • Here's the problem: the 1st-level of the Structure does have its own URL, but the child structures do not, they just exist as anchored content within the 1st-level pages. Your solution eliminates those first-level pages, too. I still think the underlying issue here is simply that Craft doesn't know how to handle URLs with anchors in them, which should simply link to the referenced anchor's position on the linked page. There is no problem with the URLs being generated by my approach, just with the routing of those URLs. Linking to an anchor position on another page is a common practice. – cmal Jul 18 '14 at 13:33
  • You can do it with the URL entries enabled as well. You just cannot call the {{entry.url}} parameter and accomplish What you're trying to do. The hash value is not passed to the server. So having a url like 'something#whatever' only passes in 'something' to the system to route the page. You can do what you're trying to accomplish, you just can't use the {{entry.url}} value. This issue on answer may shed more light on how the browser interprets hashes. – Ian Jul 18 '14 at 16:14
  • I'm curious if you've tried this. I'm creating the URL this way and the result is exactly the same as when using {{ entry.url }}: <a href="/services/{{ entry.slug }}#{{ subEntry.slug }}">{{ subEntry.title }}</a> – cmal Jul 18 '14 at 19:27
  • 1
    The answer to this ended up having nothing to do with Craft. It was being caused by the Javascript routing used for the project. Sorry to waste everyone's time. – cmal Jul 27 '14 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.