How do I use disqus comments in github pages blog (Markdown)?
The easiest and cleanest way to do it is to create a partial with the HTML that disqus provides in your _includes/
folder (e.g. _includes/disqus.html
) and then just including it in your posts layout file (e.g. _layouts/post.md
):
{% include disqus.html %}
You can see an example here: post layout and disqus partial.
Include the disqus comment in your post.html
, and set an identifier for the comment count link:
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = '<your-disqus-name>';
var disqus_identifier = '{{ page.id }}';
...
</script>
In your default.html
template include the comment count script:
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = '<your-disqus-name>';
...
</script>
Then add the link to the comments using the data-disqus-identifier
attribute, so that the comment count will show up after each post in your blog's main page:
<a href="{{post.id}}" data-disqus-identifier="{{post.id}}">Leave a comment</a>
There is an "official" way to accomplish this task. You can find the Disqus indication at this link. In details, the procedure is the following:
Add a variable called
comments
to the YAML Front Matter (i.e. the header of your post file) and set its value totrue
. A sample front matter might look like:layout: default comments: true # other options
Create a new template file (i.e.
disqus.html
) and put the Universal Embed Code there between{% if page.comments %}
and{%- endif -%}
.Include the
disqus.html
file into your post template.
Hope it helps :)
To summarise:
- Use 3rd comment service Disqus, create one its account
- Associate your site, that is your github site, with disqus
- Get Disqus shortname in
admin/settings/general/
- Edit your _config.yml of github, make sure it contains following content:
disqus: shortname: <your disqus short name>
Make sure there is
disqus.html
under_includes
and it looks like:{% if page.comments %} <div class="comments"> <div id="disqus_thread"></div> <script type="text/javascript"> var disqus_shortname = '{{ site.disqus.shortname }}'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })(); </script> <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> </div> {% endif %}
Include
disqus.html
in_layouts/post.html
To enable comment, add
comments:true
on your post yaml front matter. Disable it by settingcomments: false
or by not including the comments option at all.