have any way to change tag name dynamically in get query ? - ghost-blog

{{#get "posts" filter="tag:news" as |news_post|}}
{{#foreach news_post}}
{{title}}
{{/foreach}}
{{/get}}
How Can I Change "news" tag dynamically ?

By your question I suggest you want to base it on some that within your .. post perhaps ? You can use it within the {{post}} by using the {{tags.[0].slug}}
{{#get "posts" filter="tag:{{tags.[0].slug}}" include="tags" as |news_post|}}
{{#foreach news_post}}
- {{title}} - {{tags.[0].slug}}<br>
{{/foreach}}
{{/get}}

Related

How to list pages in Ghost CMS?

I'm trying to show pages on my homepage.
In my home.hbs I have
{{!-- The tag above means: insert everything in this file
into the {body} of the default.hbs template --}}
{{#is "home"}}
{{#if #site.description}}
<header class="page-head">
<h2 class="page-head-title">{{#site.description}}</h2>
</header>
{{/if}}
{{/is}}
{{#get "posts" filter="page:true"}}
{{#foreach posts}}
{{title}}
<p>{{excerpt words="33"}}</p>
{{/foreach}}
{{/get}}
Nothing is listed on my homepage. This works for posts but not pages for some reason.
I have the article featured, and it has "Category" tag - is it possible to display these on the homepage?
You're almost there, try this:
{{#is "home"}}
{{#if #site.description}}
<header class="page-head">
<h2 class="page-head-title">{{#site.description}}</h2>
</header>
{{/if}}
{{/is}}
{{#get "pages" limit="all"}}
{{#foreach pages}}
{{title}}
<p>{{excerpt words="33"}}</p>
{{/foreach}}
{{/get}}
Hope this helps :)

Calling a Specific custom_field BigCommerce Stencil

Currently I can get custom fields all or none. Like this
{{#each product.custom_fields}}
{{ id }} : {{ name }} : {{ value }}
{{/each}}
but what if i want to call just one of them by id or name like the below. Is there a way to do that with stencil or otherwise?
{{product.custom_fields.id:4.name}}
You can select a list item by id. {{product.custom_fields.id.4.name}}, however, if you want to select by name you'll need to implement a conditional as #alyss suggested.
See: How do I access an access array item by index in handlebars?
You can use the existing {{if}} helper to accomplish this
{{#if display_name '===' 'material'}}
{{#each product.custom_fields}}
{{id}} : {{name}} : {{value}}
{{/each}
{{/if}}

how to get all posts on post.hbs page in ghost

I want all posts on post.hbs but it's not displaying any post list, I want check title with my string and display particular post on post.hbs.
Below code is not working in post.hbs
{{#foreach posts}}
<p>{{content}}</p>
{{/foreach}}`
Its not working on post.hbs page
Post.hbs is for displaying a specific post, so you are in {{post}} scope:
{{#post}}
{{/post}}
You can try something like this:
{{#get "posts" limit="all" as |allposts| }}
{{#foreach allposts}}
{{content}}
{{/foreach}}
{{/get}}

Passing through variable into partial and using an #is helper - Handlebars

I'm trying to pass a variable (tag name) into a Handlebars partial and use an #is block helper on the tag but for some reason it just won't play ball. This is my code:
Call to my partial and passing through the tag name.
{{> nav tagged='page' }}
In the partial itself I do the following (tagged is the variable name passed through):
{{#each tags}}
{{#is tag tagged}}
{{#each pages}}
// Do code here
{{/each}}
{{/is}}
{{/each}}
If I just render the tagged variable it displays the variable value as expected so a bit confused as to why its not working.
Thanks.
The issue you have is that the tagged variable is in the parent context but you're trying to reference it within the #each tags loop.
You can reference the parent context with ../ so the working code would be
{{#each tags}}
{{#is tag ../tagged}}
{{#each pages}}
// Do code here
{{/each}}
{{/is}}
{{/each}}

Handlebars index in array to render in href

I have an array of links that I need to render into hrefs in the html. Here is what I have, I am itterating through the array with {{#each shipUrl}}:
shipUrl:{["http:urlone.com","http:urltwo.com"]}
Now if I use this:
<div>{{shipUrl}}</div>
It will evaluate to this on the page
[http:urlone.com]
[http:urltwo.com]
But if I put it in an href
<div href="{{shipUrl}}">Click</div>
This renders to [http:urlone.com] ...
What is the correct way to get the index of this object into an href?
The answer was to use {{this}}
In HTML
<div href="{{this}}">Click</div>

Resources