Last active
July 5, 2016 18:03
-
-
Save dpawluk/97e24e6de22d80b6eb96eef1d50ccc46 to your computer and use it in GitHub Desktop.
Code for creating a related articles widget using that uses labels to relate articles instead of whatever algorithm is used by default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <aside class="article-sidebar side-column"> | |
| {{recent_articles}} | |
| <!-- BEGIN CUSTOM SECTION FOR ARTICLES RELATED BY LABEL --> | |
| <section class="articles-related" id="label_related"> | |
| <h3>Related articles</h3> | |
| <img id="label_rel_spinner" src="//p5.zdassets.com/hc/theme_assets/413487/200013043/loading.gif"/> <!-- You get this URL after uploading the loading.gif to assets --> | |
| <!-- source image uploaded to imgur here - http://i.imgur.com/JPrUDDz.gifv --> | |
| </section> | |
| <!-- END CUSTOM SECTION FOR ARTICLES RELATED BY LABEL --> | |
| </aside> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // BEGIN RELATED ARTICLE CODE | |
| // Get current article id | |
| var path = window.location.pathname; | |
| if (path.indexOf("articles") > -1) { | |
| var article_id = path.split('/')[4]; | |
| function filter_results(data) { | |
| var related_info = _.map(data, function(r){ | |
| return { | |
| url: r.url, | |
| title: r.title | |
| } | |
| }); | |
| if (related_info.length > 0) { | |
| if (related_info.length > 3) related_info = related_info.slice(0,3); | |
| var html = '<ul>'; | |
| _.each(related_info, function(obj){ | |
| html += "<li><a href='" + obj.url + "'>"+ obj.title + "</a></li>"; | |
| }); | |
| html += '</ul>'; | |
| $("img#label_rel_spinner").replaceWith(html); //change this to the related article box we will be creating | |
| } | |
| else { | |
| var html = "<p> There were no related articles</p>"; | |
| $("img#label_rel_spinner").replaceWith(html); | |
| } | |
| }; | |
| function query_labels(label_array) { // Use current article labels to generate a new query for articles sharing labels | |
| var labels_string = label_array.join(','); | |
| $.ajax({ | |
| url: "/api/v2/help_center/articles/search.json?label_names=" + labels_string | |
| }).done(function(response){ | |
| filter_results(response.results); | |
| }); | |
| }; | |
| $.ajax({ // Use article id in request to retrieve current article labels | |
| url: "/api/v2/help_center/articles/" + article_id + ".json" | |
| }).done(function(response){ | |
| var label_names = response.article.label_names; | |
| query_labels(label_names); | |
| }); | |
| } | |
| // END RELATED ARTICLE CODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment