Ghost - how to get a specific page? - ghost-blog

I'm a Ghost beginner, and I know I can get the list of pages doing like below.
{{#get "pages"}}
{{#foreach pages}}
{{{html}}}
{{/foreach}}
{{/get}}
But am I able to fetch a specific page? Let's assume that I have an "about" page that I'd like to fetch in order to show its contents into the blog's sidebar, for example, this is what I tried, but it's not working.
{{#get "pages/slug/about" as page}}
{{page}} // prints undefined
{{/get}}
Any help would be much appreciated.

The first parameter passed to the #get helper specifies the name of the resource you want to query. It should be either posts, tags or authors. In your use case it should be posts.
{{#get "posts" slug="pages/slug/about" as |post|}}
{{#post}}
<h1>{{title}}</h1>
<div class="post-content"> {{content}} </div>
{{/post}}
{{/get}}

Related

Fetching all tags to make a navigation component, Ghost

I was creating a ghost blog and i created a custom component to display all the tags in the blog.
I used the below code to do so,
{{#get "tags" limit="all"}}
<ul class="tags">
{{#foreach tags}}
<li>
{{name}}
</li>
{{/foreach}}
</ul>
{{/get}}
I made the component to act as a filter. But I am facing some issues,
Tags with no posts are not displaying.
Also is there any way to know which tag filter is currently active?
can someone help me with what I am doing wrong.?
Thanks in advance.
I believe tags with no posts technically don't exist so they don't they don't create empty page routes, as all tags have their own tag page (/tag/example-tag/). As for knowing the active class I assume you're wanting to apply an active class to the tag link, which can be done with link_class:
<a class="tag {{link_class for=url class='tag-current'}}" href="{{url}}">{{name}}</a>

Ghost: Get URL of post in foreach loop

I'm looking to get the URL of a post accessed using a {{#foreach posts}}{{/foreach}} helper. The structure I am using right now is :
<div class="pfdflxbx">
{{#get "posts" limit="3" include="tags, authors" filter="featured:true"}}
{{#foreach posts limit="3"}}
{{title}}
{{/foreach}}
{{/get}}
</div>
When I insert a {{url}} helper between {{title}} and {{/foreach}}, the URL that gets returned to me is the url of the site's home page.
If I use the following structure:
<div class="pfdflxbx">
{{#get "posts" limit="3" include="tags, authors" filter="featured:true"}}
{{#foreach posts limit="3"}}
{{#post}}
{{title}}
{{/post}}
{{/foreach}}
{{/get}}
</div>
Nothing renders to the DOM, as in, no posts populate.
Could someone help shed some light on the correct practice here?
The first solution I posted was correct. There is a bug in the back end version I am working with which causes the URL of the blog posts to redirect to the welcome page.

Unsure on how to show the featured page as my homepage

I am creating a Ghost Theme and I want to have my index.hbs show the featured post as the page. I have tried a range of things but nothing seems to work:
index.hbs
{{!< default}}
{{!-- The tag above means: insert everything in this file
into the {body} of the default.hbs template --}}
{{!-- The main content area --}}
<main>
{{#foreach posts}}
{{#has featured="true"}}
{{post}}
{{/has}}
{{/foreach}}
</main>
I think the best way to achieve having a featured post as the main page would be to use Dynamic Routing and route / to the post or page with featured flag. Alternatively, you could fetch and display the featured post using the {{get}} query described here.

How to get internal tag posts in Ghost

I'm currently working on a Ghost blog, but I'm wondering how I might be able to grab all the posts that have a internal tag in Ghost.
I found
{{#get "tags" limit="all"}}
{{#foreach tags visibility="internal"}}
{{removeSubstring name '#META:'}}
{{/foreach}}
{{/get}}
this code but it only returns tag list not posts.
I want a internal tag posts list, Can anyone please help me ?
When you say internal tags do you mean Private Tags? If so then that's not really what private tags are for, you can filter with them though like so:
{{#get "posts" filter="tag:hash-tagname"}}
{{#foreach posts}}
{{title}}
{{/foreach}}
{{/get}}
To get all public tags use the following:
{{#get "tags" limit="all"}}
{{tags}}
{{/get}}
Hope this helps!

Ghost blog get featured posts on single post page

I want to show featured posts on sidebar, and I code like this:
{{#foreach posts}}
{{#if featured}}
<!-- do something -->
{{/if}}
{{/foreach}}
This works well for me on homepage, but when I turn to single post page it's broken.
Seeming that I can't use {{#posts}} on single post page?

Resources