Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Adding a Class to Article Sidebar Links

Featured Replies

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.

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 %}
  • 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.

  • 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>

=(

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 %}
  • 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.

{% assign current_url = blog.url | append: blog.title }} %}
Are the active classes used on the blog index, or the article entry page?

Edited by Jack

  • 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 }}.

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>
  • 1 month later...
  • 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! =)

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 :good:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.