December 9, 20169 yr Hi all, What I want to do is add a class (class="on") to the links in the sidebar of an article but I want only the current link to have the on class, if that makes sense. Currently I have a list of links (most recent blog posts) in a sidebar and I want the current article to be highlighted with the on class and some CSS but I'm not sure how to go about it. I have an OK understanding of liquid but just not sure how to code this? Here is the code for the sidebar links: {% for article in blog.articles limit:20 %} <li> <a href="{{ article.url }}" title="{{ article.title | escape }}">{{ article.title }} {{ page_title }}</a> </li> {% endfor %} How can I add a class to only one link depending on what article a user is on? Thanks,Al.
December 9, 20169 yr I have no way of testing whether this actually works {% for article in blog.articles limit:20 %} <li> <a href="{{ article.url }}" {% if link.active %}class="on"{% endif %} title="{{ article.title | escape }}">{{ article.title }} {{ page_title }}</a> </li> {% endfor %}
December 9, 20169 yr Author Hi Jack, Thanks for the reply mate, unfortunatley that didn't work, I was trying something similar but wasn't sure if I was on the right track. To me it makes sense to have link.active in there but it just doesn't seem to work. Any other thoughts? I had thought that maybe page.url == to article.url that might work, but don't think the page.url applies to blog articles =( Thanks,Al.
December 9, 20169 yr Author Geeting some help from someone over on the Shopify forums, he had suggested the following but nothing has worked yet. <a href="{{ article.url }}" title="{{ article.title | escape }}" {% if article.title == page_title %}class="on"{% endif %}>{{ article.title }} {{ page_title }}</a> {% assign current_url = blog.url %} <a href="{{ article.url }}" title="{{ article.title | escape }}" {% if article.url == current_url %}class="on"{% endif %}>{{ article.title }} {{ page_title }}</a> =(
December 9, 20169 yr Geeting some help from someone over on the Shopify forums, he had suggested the following but nothing has worked yet. <a href="{{ article.url }}" title="{{ article.title | escape }}" {% if article.title == page_title %}class="on"{% endif %}>{{ article.title }} {{ page_title }}</a> {% assign current_url = blog.url %} <a href="{{ article.url }}" title="{{ article.title | escape }}" {% if article.url == current_url %}class="on"{% endif %}>{{ article.title }} {{ page_title }}</a> =( That would be my recommendation too, it should work. I would output both URL's to see what they contain. Also, you need to set current_url outside of the article loop. {% assign current_url = blog.url %} {% for article in blog.articles limit:20 %} {{ article.url }} {{ current_url }} {% endfor %}
December 9, 20169 yr Author Just tried that again and this is what was being output: {{ article.url }} was displaying /blogs/news/article-handle <-- I removed the real article handle and {{ current_url }} was displaying /blogs/news *I had explained to the guy on Shopify forums that the page_title would not be the same as the article.title because we would change the page title for SEO reasons, hence why his first solution did not work.
December 9, 20169 yr {% assign current_url = blog.url | append: blog.title }} %}Are the active classes used on the blog index, or the article entry page? Edited December 9, 20169 yr by Jack
December 9, 20169 yr Author Sorry this might be a stupid question but when you say active classes you mean what I am trying to add the active class to? I was getting a warning in the back end when adding your code above, just added this in to see what it would display and was just the Blog name. {{ current_url = blog.url | append: blog.title }} I was trying to add something like this earlier, but not sure how to code it: {% if article.url | remove: "/blogs/" == article.handle %}class="on"{% endif %} I was getting the same result when using {{ article.url | remove: "/blogs/" }} and {{ article.handle }}.
December 10, 20169 yr Without being able to test anything out it's hard to say, the docs leave a lot to be desired. For example, if {{ blog.id }} is the ID of the blog entry you're on, you should be able to match it to {{ article.id }} {% assign currentPageId = blog.id %} <ul> {% for article in blog.articles limit:20 %} <li>{{ article.id }} - {{ currentPageId }}</li> {% endfor %} </ul> Otherwise, you could probably grab the current article ID, loop through a list of all articles and find one that matches. {% assign currentArticleId = article.id %} <ul> {% for article in blog.articles limit:20 %} <li>{{ article.id }} - {{ currentArticleId }}</li> {% endfor %} </ul>
January 30, 20179 yr Author Hi Jack, sorry only getting back to this now. The second section of code above looks like it will work as it was throwing out the individual article ID's and then the current article ID so that will let me add a class to a list item, the only thing is how will I code that now, adding your code above displays the ID's what I need is an if statement along the lines of, if article.id is equal to currentArticleId display class="on". EDIT: I have just added this: {% if article.id == currentArticleId %}class="on"{% endif %} And that has added the class 'on' to the current article, that look OK to you? It's working on my end, thanks so much for your help Jack, solved a problem I thought I wasn't going to get sorted! =)
January 30, 20179 yr Hi Jack, sorry only getting back to this now. The second section of code above looks like it will work as it was throwing out the individual article ID's and then the current article ID so that will let me add a class to a list item, the only thing is how will I code that now, adding your code above displays the ID's what I need is an if statement along the lines of, if article.id is equal to currentArticleId display class="on". EDIT: I have just added this: {% if article.id == currentArticleId %}class="on"{% endif %} And that has added the class 'on' to the current article, that look OK to you? It's working on my end, thanks so much for your help Jack, solved a problem I thought I wasn't going to get sorted! =) That looks fine, glad you got it sorted
Create an account or sign in to comment