List all articles for each section(s) on the same category page



Posted Feb 16, 2016

Hi there,

Zendesk Component Reference: Category with section and article list.

Background: On category page when we have more articles (more than 6) for each section/sections, then there exist a "See all link" that would take you to the main section page in order to list/show all its articles.

We have used existing Zendesk component and add a capability (JS) through which we can pull/list all the article for the section on the same category page. (without redirecting users to the main section page). Whenever user click "See all link", the next set of the articles would appear.

How to do it:

  • Add below JS to the JS section of HC:
$(document).ready(function(){

/**
@add your hc domain here
@for eg: var hc_url = 'https://testcompany.com'
*/
var hc_url = 'Your_HC_URL'

var _allarticles = [],
_sorted = [],
_artHtml = '',
_id,
_url;

var _articles = function(article){
$.ajax({
url: _url,
type: 'GET',
dataType: 'json',
success: article
});
};

// function for see all articles button in category
$('.see-all-articles').click(function(e){
e.preventDefault();
_id = $(e.target).attr('href').split('/sections/')[1].split('-')[0];

if(typeof HelpCenter.user.locale == 'undefined') {
HelpCenter.user.locale = window.location.pathname.replace('/', '').replace('?', '/').split('/')[1];
}

_url = hc_url+'/api/v2/help_center/'+HelpCenter.user.locale+'/sections/'+_id+'/articles.json';
_articles(function(data){
_allarticles = data.articles;

if(data.count>30){
for(var i = 1; i<data.page_count; i++){
_url = data.next_page;
_articles(function(data){
_allarticles = _allarticles.concat(data.articles);
_arthtml = '';
$(_allarticles).each(function(idx, itm){
if(itm.draft==true){
} else {
_arthtml = _arthtml + '<li class="'+(itm.promoted==true?'article-promoted':'')+'"><span data-title="Promoted article" style="'+(itm.promoted==false?'display:none':'')+'">★</span><a href="'+itm.html_url+'">'+itm.title+'</a></li>';
}
});
$(e.target).parent().find('ul.article-list').html(_arthtml);
$(e.target).hide();
})
}
} else {
_arthtml = '';
$(data.articles).each(function(idx, itm){
if(itm.draft==true){
} else {
_arthtml = _arthtml + '<li class="'+(itm.promoted==true?'article-promoted':'')+'"><span data-title="Promoted article" style="'+(itm.promoted==false?'display:none':'')+'">★</span><a href="'+itm.html_url+'">'+itm.title+'</a></li>';
}
});
$(e.target).parent().find('ul.article-list').html(_arthtml);
$(e.target).hide();
}

});
});
// function for see all articles button in category ends here

});

 




  • There is a variable named var_hc, add your Zendesk URL there.

            for an e.g: var_hc='https://support.zendesk.com'

  • Save and publish the changes.
  • Click see all articles link.

Let us know if you face an issue.

Cheers,

Diziana

 

 


0

49

49 comments

To further modify the script so that all CSS is retained including promoted links (like Randy Scheck wanted) I modified the following line:

_arthtml = _arthtml + '<li class="'+(itm.promoted==true?'article-list-item':'article-list-item')+'">
<span class="icon-star" data-title="Promoted article" style="'+(itm.promoted==false?'display:none':'')+'">&nbsp;</span>
<a class="article-list-link" href="'+itm.html_url+'">'+itm.title+'</a></li>';

 

0


Hi Brooke - 

I'm sorry you haven't gotten an answer yet. If you haven't found resolution on it elsewhere, you could try posting it as a new question in the Guide Q&A topic to give it more visibility. 

0


Hi I'm not a coder but I've managed to implement this suggestion and it works great.

The only thing is if we have articles flagged as internal (visible to agents and managers only) the little lock icons disappear when you expand the list.

The code on the .hbs page for that is:
{{#if internal}}
<span class="icon-lock" title="{{t 'internal'}}"></span>
{{/if}}

but I'm not sure how to get that working with the example above. I'm assuming it would be somewhere around this part:

$(_allarticles).each(function(idx, itm){
if(itm.draft==true){
} else {
_arthtml = _arthtml + '<li class="article-list-item'+(itm.promoted==true?' article-promoted':'')+'"><span data-title="Promoted article" style="'+(itm.promoted==false?'display:none':'')+'">★</span><a href="'+itm.html_url+'">'+itm.title+'</a></li>';
}
});

Can anyone help me?

0


I still do not see any solutions of how to show all articles on category page without clicking on button.

0


Hi @Andrea Nugnes, can you share the screenshot of the error it is showing to you when you are adding the code and it is not working for you?

Thanks

0


Hi @Nerea Garcia Ramila, Can you please share the URL of your HC so that I can share how to add space between the articles?

Thanks

0


P.s.: I also formulate a different question: 

I'd like that, after clicking on the triangle, would be shown the articles in the subsection. I attach a quick example: 

The page in question:

https://support.magicstore.cloud/hc/it/categories/115000039291-MagicStore

0


It doesn't work to me. When I click to "See all articles", nothing happens. 

Is it because I got a different chunk of code to show the articles? Instead, this chunk of code in question shows articles in a new tab. 

My support:

https://support.magicstore.cloud/hc/it/

And category page which I'm talking about:

https://support.magicstore.cloud/hc/it/categories/115000039291-MagicStore

Can you give it an eye or help me in any other way please? 

Thank you. 

0


Hi! Thanks for this code and post.

I was wondering how to maintain the spacing I have in between articles before clicking on 'See All 7 Articles'. Once I click on the link, all hidden article titles appear but they appear with very little space in between. 

Before clicking:

After clicking:

0


Thanks for coming back and letting us know that you got that sorted out and for sharing the solution, Hedda!

0


Sign in to leave a comment.

Didn't find what you're looking for?

New post