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

推荐订阅源

D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
L
LangChain Blog
B
Blog
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
U
Unit 42
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
爱范儿
爱范儿
小众软件
小众软件
I
InfoQ
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
C
Check Point Blog

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 Create a Custom Navigation Template in Ghost
Bright Themes · 2023-10-01 · via Example blog

Little things can make a big difference. Your website navigation is a great example. The navigation structure and UX have a huge impact on conversions and bounce rates. Clear, hierarchical website navigation will help your visitors find what they want and provide a great user experience.

In Ghost CMS, you can define your menu items in the Admin area by going to Settings > Navigation There you have a primary and secondary navigation section, and these are rendered using a built-in helper {{navigation}}.

The navigation helper & properties

The {{navigation}} helper outputs a formatted HTML of menu items defined in the Ghost admin panel. the default navigation template:

<ul class='nav'>
  {{#foreach navigation}}
    <li class='{{link_class for=(url) class=(concat 'nav-' slug)}}'><a
        href='{{url absolute='true'}}'
      >{{label}}</a></li>
  {{/foreach}}
</ul>

As you can see some special attributes can be used within the {{#foreach navigation}} loop of this template to help with formatting. The available attributes:

  • {{label}} - The text to display for the link
  • {{url}} - The URL to link to
  • {{current}} - Boolean true / false - whether the URL matches the current page
  • {{slug}} - Slugified name of the page, eg contact-us.

Outside of the navigation helper, you have some global attributes to check whether there are any items defined:

{{#if @site.navigation}}
  {{! Render primary menu or button }}
{{/if}}

{{#if @site.secondary_navigation}}
  {{! Render secondary menu or button }}
{{/if}}

As you can see, Ghost uses the same template for both primary and secondary navigation, but it's possible to render them differently.

Changing the navigation template

In case you want to make a custom navigation layout, maybe a fixed menu, or you simply want to render it differently, then you have to overwrite the default template. This can be done by creating a new file in your theme in the partials directory called navigation.hbs. If such a file exists, Ghost will load it instead of the default template.

For a different secondary navigation you can add the following check in the template:

{{#if isSecondary}}
  {{! Template for secondary navigation }}
{{else}}
  {{! Template for primary navigation }}
{{/if}}

Modifying the {{navigation}} helper you are not limited to render only the items defined in the admin area, you can add any valid HTML to this template. An advanced use case would be adding a mega menu or dropdown menu for your site. Ghost doesn't support this feature, so the only possibility (without using javascript) would be to add the HTML markup in this template.

Adding icons to your navigation items

Another use case could be, if you want to add icons to your navigation items dynamically, as we did for our Dashi theme. What we need for this, before customizing the navigation helper, is to add an icon sprite(svg) to our Ghost theme. This SVG sprite has to contain all icons we need for our navigation items.

The code for the navigation would look like this:

{{#foreach navigation}}
  <li class='nav-{{slug}}' role='menuitem'>
    <a href='{{url}}'>
      <svg class='icon-svg'>
        <use xlink:href='{{asset "menu-icons.svg"}}#{{slug}}'></use>
      </svg>
    </a>
  </li>
{{/foreach}}

This code assumes you have a file named menu-icons.svg in the assets directory. (example of SVG sprite from feathericons) SVG sprites contains <symbol> elements with different ids.

Caution

Important is that your navigation items' slug has to match a symbol element id.

Make sure to check your menu slugs and create/rename the corresponding icons in your SVG.

Here is how the result looks in the Dashi theme:

Dashi Ghost - Menu Icons
Dashi Ghost - Menu Icons

This guide does not provide styling for the icons/svg so make sure you apply at least width and height for the .icon-svg class.