Search code examples
htmlexpressionengine

how do I create internal links in expression engine?


I have a list of entries and I am trying to create at the top a list of titles that can be clicked on so the page will scroll to the body. The following is not working because when I use entry_id it is writing out the entire URL/entry_id

<div class="list">
    {exp:channel:entries channel="mychan" category="2"}    
        <div class="mytitle"><a href="{entry_id}">{title}</a></div>
    {/exp:channel:entries} 
</div>

{exp:channel:entries channel="mychan" category="2"}    
    <div class="ele">
        <div class="mytitle"><a href="#entry_id">{title}</a></div>
        <div class="mybody">{mybody}</div>
     </div>
{/exp:channel:entries}   

Solution

  • It's not writing out the entire URL/entry_id. The href is only the entry_id, which the browser automatically converts to http://your-site.com/entry_id. For an internal link, it needs have a # symbol.

    Also, internal link names have some other requirements:

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

    So they can't just be a number; you need to start with a letter at least.

    So in your first {exp:channel:entries} loop linking to the entry, change the link href to this (or something similar):

    <a href="#entry_{entry_id}">{title}</a></div>
    

    And in the second loop, repeat that (without the #) as an ID on your wrapping div:

    <div class="ele" id="entry_{entry_id}">
      ...
    </div>