{"id":512,"date":"2024-11-10T07:27:32","date_gmt":"2024-11-10T07:27:32","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=512"},"modified":"2024-11-10T07:27:32","modified_gmt":"2024-11-10T07:27:32","slug":"hide-posts-by-tag-in-ghost","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=512","title":{"rendered":"How to Hide Posts by Tag in Ghost: A Beginner\u2019s Guide"},"content":{"rendered":"<p>If you&#8217;re using Ghost as your blogging platform and want to hide specific posts based on tags, you can do so by modifying your theme files.<\/p>\n<p>I use Ghost.org managed ghost platform with doks theme for one of my blogs and I had to hide few posts from the blog listing page.<\/p>\n<p>This guide will walk you through the simple steps to achieve this using the <code>{{#has}}<\/code> helper which I used in my setup to hide the posts.<\/p>\n<h2 id=\"why-hide-posts-by-tag\">Why Hide Posts by Tag?<\/h2>\n<p>There can be many reasons to hide a post on a specific page. For example, I had a use case where I needed to hide a few posts that were off-topic for my audience, while still keeping them accessible on my Ghost blog.<\/p>\n<p>Also, for example, if you have posts tagged as \u201c#deals\u201d or \u201c#promos\u201d that you don&#8217;t want visible to all visitors, you can filter these out with a small code tweak.<\/p>\n<p>The need to hide posts varies depending on user requirements<\/p>\n<h2 id=\"steps-to-modify-your-theme-files\">Steps to Modify Your Theme Files<\/h2>\n<p>To implement this, you need to modify your theme files because Ghost does not have built-in functionality for this.<\/p>\n<blockquote><p>Note: If you dont know how to modify your theme files, I would suggest you to get help from developers.<\/p><\/blockquote>\n<h3 id=\"step-1-locate-your-theme-file\">Step 1: Locate Your Theme File<\/h3>\n<p>First you need to locate you theme file where it contains the code for the homepage, blog page or the section where you display posts.<\/p>\n<p>For example, for me the blog page code was located in <code>blog-grid.hbs<\/code>.<\/p>\n<p>If you have doubts, you can contact the theme owner to understand the file that has the post display logic.<\/p>\n<h3 id=\"step-2-find-the-foreach-posts-block\">Step 2: \u00a0Find the {{#foreach posts}} Block<\/h3>\n<p>Once you locate the file, look for the <code>{{#foreach posts}}<\/code> block, which is responsible for looping through and displaying your posts.<\/p>\n<p>You can use a code editor to search effectively.<\/p>\n<h3 id=\"step-3-modify-the-code\">Step 3: Modify the Code<\/h3>\n<p>Add the <code>{{^has}}<\/code> helper to check for specific tags and hide posts that match.<\/p>\n<p>Lets say your origial code block looks like the following.<\/p>\n<pre><code>{{#foreach posts}}\n    &lt;div data-pagination-post class=\"{{post_class}}\"&gt;\n        &lt;!-- Post content --&gt;\n    &lt;\/div&gt;\n{{\/foreach}}<\/code><\/pre>\n<p>Now lets say you want to hide posts with multiple tags. For example, #tag1, #tag2, #tag3 etc.<\/p>\n<p>You need to modify the code as given below by adding the <code>{{^has}}<\/code> \u00a0helper.<\/p>\n<pre><code>{{#foreach posts}}\n    {{^has tag=\"#tag1, #tag2, #tag3, #tag4, #tag5, #tag6, #tag7\" }}\n        &lt;div data-pagination-post class=\"{{post_class}}\"&gt;\n            &lt;!-- Post content --&gt;\n        &lt;\/div&gt;\n    {{\/has}}\n{{\/foreach}}<\/code><\/pre>\n<p>The <code>{{^has}}<\/code> helper checks if a post does <strong>not<\/strong> have any of the specified tags in a single list. This ensures that <strong>posts with any of the listed tags are excluded<\/strong> from rendering.<\/p>\n<p>The <code>^<\/code> symbol is used for negation, meaning \u201cnot\u201d in this context.<\/p>\n<h2 id=\"applying-this-to-other-templates\">Applying This to Other Templates<\/h2>\n<p>If you need to hide posts with certain tags on other pages, like author or tag pages, you can apply the same method to <code>author.hbs<\/code> and <code>tag.hbs<\/code> templates.<\/p>\n<h2 id=\"final-steps\">Final Steps<\/h2>\n<p>After updating your code, save the file.<\/p>\n<p>Ensure you have the backup of your website.<\/p>\n<p>Now, Zip your theme folder and upload it back to Ghost via <code>Settings &gt; Theme<\/code>.<\/p>\n<p>Refresh your homepage and check to ensure that posts with the specified tags are hidden. If you have enabled caching, ensure you clear the cache.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Using the <code>{{^has}}<\/code> helper in Ghost makes it easy to control which posts appear on your site based on tags.<\/p>\n<p>This simple customization can help tailor the visibility of your content and create a more refined user experience.<\/p>\n<p>If you need any help \u00a0or facing any issues, drop a comment and we will help you out.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/hide-posts-by-tag-in-ghost\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Hide Posts by Tag in Ghost: A Beginner\u2019s Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/hide-posts-by-tag-in-ghost\/<\/p>\n","protected":false},"author":1,"featured_media":513,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=512"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/512\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/513"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}