Show promoted articles of viewing category on category template
Kudos to @Ed Ball for the idea.
Zendesk {{promoted_articles}} component lists all the articles which you promote from your knowledge base, however, if you want to show promoted articles on category template, but only the promoted articles of that category then, here we go with the code.
Go to the HTML of your category template, copy and paste this code at the bottom of your category template -
<div class="container">
<div class="promoted-articles-container">
<section class="articles">
<h3>{{t 'promoted_articles'}}</h3>
<ul class="article-list promoted-articles"></ul>
</section>
</div>
</div>
<style>
.promoted-articles-container {display: none;}
</style>
<script>
$.getJSON('/api/v2/help_center/en-us/categories/'+{{category.id}}+'/articles.json', function(data){
var articlesList = '';
$.each(data.articles, function(idx, itm){
if (itm.promoted==true) {
articlesList += '<li class="promoted-articles-item">';
articlesList += '<a href="'+itm.html_url+'">'+itm.name+'</a>';
articlesList += '</li>';
}
});
$('ul.promoted-articles').html(articlesList);
var articlesLength = $('ul.promoted-articles > li').length;
if (articlesLength) {
$('.promoted-articles-container').show();
}
});
</script>
Save and publish the code.
Hurray! you are done.
Edit: Updated code to hide the promoted article section unless there is a promoted article in the category.
Let us know if you face any issue.
Also, give us your feedback in the comment section below :)
Enjoy!
Team Diziana
-
So I am a little late to this, but thanks for this.
And to make it even a little better I submit this...
Can this section be hidden completely if there are no articles promoted for a category?
So Billing has one promoted article. When that article has the promoted check removed then there would be no promoted articles for billing. So currently there would be a promoted article section with nothing in it. If that could be hidden from the view unless there is a promoted article in the category that would be even more awesome.
-
Hi @Ed Ball,
Thanks for the feedback. I have updated the code to hide promoted article section unless there is a promoted article in the category to make it even more awesome :)
Please sign in to leave a comment.
2 Comments