惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
量子位
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
Y
Y Combinator Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog
B
Blog
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
博客园 - 聂微东
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to use Code Injection in Ghost How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS
How to Add Tag Dropdown to Your Ghost Theme
Bright Themes · 2022-05-30 · via Example blog

There are a lot of ways you could list your tags in Ghost, one of the most common ways is creating a tag cloud (or a list of tags) while this is fine option it can take up a lot of space.

Dropdowns come in handy in this case, they conserve screen space and being a standard widget users know how to deal with them. In this tutorial we'll be using the standard way of creating a dropdown list, namely the select tag.

Build the dropdown

Let's start with the basic markup of a select tag:

<label for="select-tags">Tags:</label>
<select name="tags" id="select-tags">
  <option value="/">Option 1</option>
</select>

First we have a label describing our dropdown list, then the select containing one or more options which will appear within our dropdown.

Next step is to add a list of tags as options for our select and this is easy to do using the {{get}} helper:

<label for='select-tags'>Tags:</label>
<select
  name='tags'
  id='select-tags'
  onchange='window.location.href = event.target.value'
>
  {{#get 'tags' include='count.posts' order='count.posts desc' limit='10'}}
    {{#foreach tags}}
      <option value='/tag/{{slug}}'>{{name}}</option>
    {{/foreach}}
  {{/get}}
</select>

The {{get}} helper will fetch the 10 tags with most posts. The {{foreach}} helper will loop through those tags and create an option where the value will be the link to that specific tag and the text will be the name.

The onchange event handler will make sure that when an option is selected the user is redirected to the corresponding tag page.

Optimize the dropdown

We want to use this dropdown as a way to navigate between the categories(tags), it's important to know that the first option (or the one with the selected attribute) is going to be displayed as the default value for the dropdown. On the homepage this should be something general, like "All" or "Show all". When you click on a tag from the list, it should navigate to the tag page and there we expect the current active tag to show as the default value for the dropdown.

To achieve this we have to tweak it a bit so the behavior of the element makes sense as explained before:

<label for="select-tags">Tags:</label>
<select name="tags" id="select-tags" onchange="window.location.href = event.target.value">
  <option value="/" {{^is "tag"}}selected{{/is}}>{{t "Show all"}}</option>
  {{#if tag}}
    <option value="/tag/{{tag.slug}}" selected>{{tag.name}}</option>
  {{/if}}
  {{#get "tags" include="count.posts" order="count.posts desc" limit="20"}}
    {{#foreach tags}}
      <option value="/tag/{{slug}}">{{name}}</option>
    {{/foreach}}
  {{/get}}
</select>

The first option is going to be "Show all", which points to the home page. We check the context and if the current context is not "tag" then this option will be the default: {{^is "tag"}}selected{{/is}}.

If this is a tag context however then the current tag will be set as the default option:

{{#if tag}}
  <option value='/tag/{{tag.slug}}' selected>{{tag.name}}</option>
{{/if}}

Now we should have the list as we wanted. One last detail to avoid duplicate values in the dropdown, will be solved with javascript:

  <script>
    const options = [];

    document.querySelectorAll('#select-tags > option').forEach(function(option) {
      options.includes(option.value) ? option.remove() : options.push(option.value);
    });
  </script>

The final result

This exact approach is implemented in our Dashi Ghost Theme. In this case the label is an icon and there is css applied to the elements. Here is how it looks:

Tag Dropdown
Tag Dropdown
Tag Dropdown
Tag Dropdown

You can see it live on the Dashi Demo. I hope this was helpful.