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

推荐订阅源

WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
D
DataBreaches.Net
腾讯CDC
V
Visual Studio Blog
博客园 - 叶小钗
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
U
Unit 42
博客园 - 司徒正美
博客园 - 聂微东

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 Featured Post Slider in Ghost
Bright Themes · 2023-02-15 · via Example blog

Having a featured post section on the homepage is a good way to highlight some of your content and with the featured flag in the post settings it's very easy to do.

But this needs some adjustment within your theme and more specifically you have to edit your index.hbs or home.hbs file.

Let's start with a simple scenario and simply get a list of featured posts and render it before the rest of the posts:

{{! Featured posts }}
{{#get 'posts' filter='featured:true' limit='all' as |featured|}}
  {{#foreach featured visibility='all'}}
    <h2><a href='{{url}}'>{{title}}</a></h2>
  {{/foreach}}
{{/get}}

In the above code block the following happens:

  • the {{get}} helper is used to fetch the list of featured posts using the filter attribute
  • in this case all featured posts will be fetched but you can also set a limit using the limit attribute
  • using the {{foreach}} helper we loop through all the posts and render the post title
  • you can extend this with other attributes within the post context such as date, author ( here is the full list of post attributes

To move this section to the top of the page, you have to add this code block before the other posts are displayed. This depends on your current setup, but generally it should be in the first part of your index.hbs file.

Now let's see a more specific scenario where we will create a slider/carousel out of the featured posts. For this we will use a library called Tiny Slider.

We have to include the tiny-slider script and css for this to work and the easiest way is to include it using a CDN. For this you can add the following in Code Injection:

<script
  src='https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.3/min/tiny-slider.js'
  integrity='sha512-D/zaRVk05q6ERt1JgWB49kL6tyerY7a94egaVv6ObiGcw3OCEv0tvoPDEsVqL28HyAZhDd483ix8gkWQGDgEKw=='
  crossorigin='anonymous'
></script>
<link
  rel='stylesheet'
  href='https://cdnjs.cloudflare.com/ajax/libs/tiny-slider/2.9.3/tiny-slider.css'
  integrity='sha512-eMxdaSf5XW3ZW1wZCrWItO2jZ7A9FhuZfjVdztr7ZsKNOmt6TUMTQgfpNoVRyfPE5S9BC0A4suXzsGSrAOWcoQ=='
  crossorigin='anonymous'
/>

Another possibility to integrate this in your Ghost theme development setup, we use gulp as a task runner so it's possible to install tiny slider as a dependency and integrate it in the normal theme workflow. (if you feel comfortable enough with gulp and javascript)

Next step is to fetch the featured posts again and this time include the post image in the rendered template (you can add other properties from the post context as well).

{{! Featured posts }}
{{#get 'posts' filter='featured:true' limit='all' as |featured|}}
  <div class='featured-feed js-featured-feed'>
    {{#foreach featured visibility='all'}}
      <div class='featured-card'>
        {{! Feature Image }}
        {{#if feature_image}}
          <a
            class='featured-card__media m-b-sm'
            href='{{url}}'
            title='{{title}}'
            aria-label='{{title}}'
          >
            <img
              class='lazyload'
              data-srcset='{{img_url feature_image size='s'}} 300w,
                      {{img_url feature_image size='m'}} 600w'
              srcset='data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
              data-sizes='auto'
              data-src='{{img_url feature_image size='s'}}'
              src='{{img_url feature_image size='xxs'}}'
              alt='{{title}}'
            />
          </a>
        {{/if}}

        {{! Post Title }}
        <h2 class='fw-600 m-b-0 text-acc-1 text-xl'>
          <a href='{{url}}' aria-label='{{title}}'>{{title}}</a>
        </h2>
      </div>
    {{/foreach}}
  </div>
{{/get}}

This code will fetch the featured posts and then display the feature image and the title for all those posts.

The last step is to initialize the library. Include the following lines right after the closing {{/get}} tag:

{{#contentFor 'scripts'}}
  <script>
    const sliderContainer = document.querySelector('.js-featured-feed'); if
    (sliderContainer) { const slider = tns({ container: sliderContainer, items:
    3, slideBy: 1, loop: true, autoplay: false, gutter: 32, nav: true,
    responsive: { 0: { items: 1, }, 768: { items: 2, }, 992: { items: 3, } } });
    }
  </script>
{{/contentFor}}
  • the {{#contentFor}} tag will put the content within it in the {{{block "scripts"}}} which normally should be in the default.hbs file, towards the end. (more about the block helper)
  • the sliderContainer is needed for the tiny slider setup (it's the element containing all the posts)
  • tns is the slider itself, when we want to initialize it, we have to provide options like:
    • items - how many items should be displayed
    • autoplay - whether the plugin will automatically slide the elements
    • gutter - the spacing between the elements
    • responsive - you can set breakpoints to display items based on the device (in the case above on mobile only one item is displayed and as the viewport grows 2 or even three items are displayed)

There are much more options available to tweak the library behavior. ⟶ List of options

Result and Demo

Now your featured slider should be ready and integrated in your Ghost theme. In our Dashi theme we have this plugin integrated and styled.

Here is the result:

Featured Slider
Featured Slider

See it live