Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Related posts not shown #554

Closed
3 of 5 tasks
fa-ribeiro opened this issue Sep 25, 2016 · 27 comments
Closed
3 of 5 tasks

Related posts not shown #554

fa-ribeiro opened this issue Sep 25, 2016 · 27 comments

Comments

@fa-ribeiro
Copy link
Contributor

  • This is a question about using the theme.
  • I believe this to be a bug with the theme --- not Jekyll, GitHub Pages or one of the bundled plugins.
  • This is a feature request.
  • I have updated all gems with bundle update.
  • I have tested locally with bundle exec jekyll build.

Environment informations

  • Minimal Mistakes version: 4.0.1 (feature/theme-gem branch)
  • jekyll gem version: 3.2.1 (master branch)
  • Operating system: Mac OSX 10.11.6

Expected behavior

Related post should appear on every post as they are enabled by default in my _config.yml:

# Defaults
defaults:
  # _posts
  * scope:
      path: ""
      type: posts
    values:
      related: true

Steps to reproduce the behavior

  1. create a couple of posts
  2. enable related posts on _config.yml

Additional information

If I insert the following code block (extracted from _layouts/single.html) in a post, the related posts are shown as expected:

{% if page.id and page.related and site.related_posts.size > 0 %}
  <div class="page___related">
    {% if site.data.ui-text[site.locale].related_label %}
      <h4 class="page__related-title">{{ site.data.ui-text[site.locale].related_label | default: "You May Also Enjoy" }}</h4>
    {% endif %}
    <div class="grid__wrapper">
      {% for post in site.related_posts limit:3 %}
        {% include archive-single.html type="grid" %}
      {% endfor %}
    </div>
  </div>
{% endif %}

Any ideas?
Thanks in advance for any help!

@mmistakes
Copy link
Owner

Link to a repo so I can troubleshoot? Most likely it's a content or config problem on your end.

@fa-ribeiro
Copy link
Contributor Author

fa-ribeiro commented Sep 25, 2016

it's a kinda private repo, but here it is the _config.yml: _config.yml.zip

@mmistakes
Copy link
Owner

Config looks fine. Could be content or something else but since I can't see that to test hard to say.

@fa-ribeiro
Copy link
Contributor Author

fa-ribeiro commented Sep 25, 2016

ok, possibly it's something wrong somewhere... I'll review everything again.
Many thanks for you help.

@mmistakes
Copy link
Owner

mmistakes commented Sep 26, 2016

Also just noticed you're using the feature/theme-gem version of the theme. Since it's not officially released it's possible there's a bug in there, but I'd be surprised if that was the case as the layouts haven't been modified.

What happens if you pull down the latest stable release and drop in the contents of your _posts folder?

Since it's working fine on the demo site I have a suspicion there's something going on with the theme in gem form. Or site.related_posts is being picky on what posts it determines are related. On the demo site for example there are a few posts that don't display any related items even though related: true.

@fa-ribeiro
Copy link
Contributor Author

Yes, I'm testing the feature/theme-gem branch.
My Gemfilehas:

gem "jekyll", :git => "https://github.com/jekyll/jekyll.git"
gem "minimal-mistakes-jekyll", :git => "https://github.com/mmistakes/minimal-mistakes.git", :branch => "feature/theme-gem"

I copied the posts from the minimal-mistakes/example/_posts/ (again the feature/theme-gem branch) into my _posts. As result, the latest 4 posts have related posts, but not the others; as soon as I remove the added posts, no more related posts are shown.

@mmistakes
Copy link
Owner

Did some testing of my own and there are some posts in the demo site that don't show related posts even though they should. Adding {{ site.related_posts | inspect }} to _layouts/single.html shows outputs:

this for a post with related

[#<Jekyll::Document _posts/2016-02-24-welcome-to-jekyll.md collection=posts>, #<Jekyll::Document _posts/2013-08-16-markup-syntax-highlighting.md collection=posts>, #<Jekyll::Document _posts/2013-05-22-markup-more-images.md collection=posts>, #<Jekyll::Document _posts/2013-01-11-markup-html-tags-and-formatting.md collection=posts>, #<Jekyll::Document _posts/2013-01-10-markup-image-alignment.md collection=posts>, #<Jekyll::Document _posts/2013-01-09-markup-text-alignment.md collection=posts>, #<Jekyll::Document _posts/2013-01-05-markup-title-with-special-characters.md collection=posts>, #<Jekyll::Document _posts/2013-01-05-markup-title-with-markup.md collection=posts>, #<Jekyll::Document _posts/2012-05-22-markup-text-readability.md collection=posts>, #<Jekyll::Document _posts/2012-03-15-layout-sidebar-custom.md collection=posts>]

and this for ones without

nil

I don't really know how Jekyll determines what is related and what isn't but for that appears to be where the bug or problem is. I suspect it's content related but not sure why that would matter. Sites like @jhabdas's appears to work just fine so it doesn't appear to be the theme's fault.

Where it gets even stranger is only the most 4 recent posts on the demo site show related posts. I took the most recent post (which is showing related posts), changed the date so it became the last post in the list, and the related posts disappeared.

Thought maybe limit: 4 has something to do with it in this line {% for post in site.related_posts limit:4 %} so I took that out too, and it made no difference.

I'm really stumped here. Only thing I can think of would be to not use site.related_posts and instead use site.posts and filter on tags/categories instead to fake "related posts".

@ghost
Copy link

ghost commented Sep 26, 2016

Scroll down on any post on https://habd.as to see the related posts feature and feel free to inspect my blog source code: https://github.com/jhabdas/habd.as. Good luck!

@mmistakes
Copy link
Owner

@jhabdas I'm stumped why it's working fine for you but not the MM's demo site. I wrote the damn thing and I can't figure out what's going wrong 😅

It's really strange. After the 4th most recent post they stop showing. I thought maybe it was working for you because you're using a newer Jekyll gem, but looks like it's the older GitHub Pages one.

And in @fa-ribeiro's case their using the latest Jekyll off of master.

So strange!

@ghost
Copy link

ghost commented Sep 26, 2016

I wrote the damn thing and I can't figure out what's going wrong

ROFL

Might be worth leaving this open and see if anyone else is having issues besides @fa-ribeiro. Also, @mmistakes when you're ready to come to the dark side I'd love to work together to port this theme to Hugo.

@mmistakes
Copy link
Owner

@jhabdas That's what I was thinking. If it's a widespread issue and not something to do with a weird content edge-case I'm surprised I hadn't heard about yet.

RE: Hugo. I've looked at it and considered switching my personal site over just because it takes so long to build. Everyone always says it builds faster than Jekyll when you have a lot of posts but it would be a crap ton of work for me to rebuild just to test that. So I haven't. PR's like this give me faith Jekyll's build speed will improve.

I've made a lot of improvements pulling out anything that isn't strictly for converting .md files into .html files, and that helped a lot.

Not to mention I didn't really care for the template syntax. It's close enough to Liquid sure, but it just looks ugly to me.

@icicimov
Copy link

icicimov commented Oct 6, 2016

Hi guys,

Maybe unrelated but I have the same issue in my gh-pages branch I forked from the master about 5-6 days ago. I have the following in my _config.yml file:

defaults:
  # _posts
  - scope:
      path: ""
      type: posts
    values:
      layout: single
      read_time: true
      comments: true
      share: true
      related: true
      author_profile: true

but related posts is not showing on any of my posts, see https://icicimov.github.io/blog/

I was under impression this should be working by default but maybe I'm missing something. I'm not sure how the plugin/feature works ie is it parsing the posts body to find a match based on some words or is just picking up 4 random posts etc. Probably I'm just missing something, some non-minimal mistake :-(

Thanks,
Igor

@mmistakes
Copy link
Owner

mmistakes commented Oct 6, 2016

I believe there may be a bug in Jekyll core and how it handles related posts.

The theme is using this to output 4 related posts, which is wrapped in a conditional that only shows the module when there are related posts and the feature is enabled.

{% for post in site.related_posts limit:4 %}
  {% include archive-single.html type="grid" %}
{% endfor %}

When I remove limit:4 from the for loop related posts works again, but it shows 10 related posts (which is the default behavior). For whatever reason the limit is limiting it to the first 4 posts of the site, instead of limiting it to showing 4 related posts on a given post... if that makes sense.

For example the 4 most recent posts show related posts fine, but when you hit say the 5th post there are no related posts shown. The limit:4 is affecting that when it really shouldn't.

There's probably another way to limit the amount of related posts shown that I'll investigate to get around the issue.


As far as what related_posts uses to parse, I'm not entirely sure. Because GitHub Pages disables LSI (latent semantic indexing) I'm pretty sure it outputs an array of the 10 most recent posts, which makes the whole thing pointless. On my personal site I use a plugin that provides better matches for this reason.

@mmistakes
Copy link
Owner

I came up with a way of showing "related posts" by like post.tags instead of using the built in site.related_posts array.

The advantage here is you actually get a different set of related posts on each post instead of just the "most recent" posts that currently happens due to LSI being disabled on GitHub Pages.

Using this spaghetti code of Liquid in _layouts/single.html will display related posts that share the same tags as the current post. It will show 1-4 posts depending on how many matches it finds.

 {% comment %}<!-- show posts by related tags when `related: true` -->{% endcomment %}
  {% if page.related %}
    {% assign n_posts = 0 %}
    {% for post in site.posts %}
      {% if post.id == page.id %}{% continue %}{% endif %}
      {% for this_tag in page.tags %}
          {% if post.tags contains this_tag %}
            {% if n_posts == 0 %}
              <div class="page__related">
                {% if site.data.ui-text[site.locale].related_label %}
                  <h4 class="page__related-title">{{ site.data.ui-text[site.locale].related_label | default: "You May Also Enjoy" }}</h4>
                {% endif %}
                <div class="grid__wrapper">
            {% endif %}
            {% include archive-single.html type="grid" %}
            {% assign n_posts = n_posts | plus: 1 %}
            {% break %}
          {% endif %}
      {% endfor %}
      {% if n_posts > 3 %}{% break %}{% endif %}
    {% endfor %}
    {% if n_posts > 0 %}
        </div>
      </div>
    {% endif %}
  {% endif %}

Thoughts?

@icicimov
Copy link

icicimov commented Oct 6, 2016

@mmistakes , thanks for the code it works for me, tried and applied to my site. Matching by tags looks like right direction to go in terms of logic about finding related posts if you ask me.

At the end, just wanted to say thanks for this awesome theme!

@mmistakes
Copy link
Owner

@icicimov Great! Yeah I think related posts by tags does a better job of conveying "related posts" than whatever basic method the default uses.

@mmistakes mmistakes modified the milestone: 4.0 Oct 7, 2016
kimoppalfens pushed a commit to kimoppalfens/kimoppalfens.github.io that referenced this issue Dec 12, 2016
@justinrummel
Copy link
Contributor

Trying to convert my site to the Gem version of MM. Was this ever added to the Gem version or should I copy my old single.html to the _layouts/ folder?

@ghost
Copy link

ghost commented Dec 24, 2016

@justinrummel would love to hear more about your experience once you make the leap!

@mmistakes
Copy link
Owner

@justinrummel No. This is still open as I haven't found a fix.

If you're using the gem version remove all local _layouts, _includes, and _sass folders and files. Unless of course you want them to override the ones bundled in the gem.

@justinrummel
Copy link
Contributor

@mmistakes and @jhabdas the migration is complete. Wasn't as bad as I thought except for all the images moving from /images/ to /asset/images/. Took me a while to figure out what was doing what (inserting the default vs. my .md files forcing a path). Thank goodness for find and perl commands.

~/mm-gem-repo/> perl -p -i -e 's/(^    image: )/    image: \/assets\/images\//g' _posts/*
~/mm-gem-repo/> perl -p -i -e 's/(^    overlay_image: )/    overlay_image: \/assets\/images\//g' _posts/*
~/mm-gem-repo/> perl -p -i -e 's/(^    teaser: )/    teaser: \/assets\/images\//g' _posts/*

If there was something still wrong, I would search the origin HTML to see if I could find something unique and then search the repo for that value:

~/mm-gem-repo/> find . -type f -exec grep -e 'mySearchItem' \{\} \; -print

From there I had to find what didn't function or look right. I inserted Instagram images on the main page, added .css for "fifth" and "sixth" for large amount of photosets pointing to flickr, added Lunr search functionality, and finally this section for related items. I prefer the searching on tags vs. what is default right now. I guess I'll leave the single.html until something permanent takes it place.

My slimmed down repo is:

_includes
_layouts
_sass

@justinrummel
Copy link
Contributor

While researching HTTPS testing via jekyll serve, I noticed there is a --lsi flag. From Jekyll's documentation:

If the page being processed is a Post, this contains a list of up to ten related Posts. By default, these are the ten most recent posts. For high quality but slow to compute results, run the jekyll command with the --lsi (latent semantic indexing) option. Also note GitHub Pages does not support the lsi option when generating sites.
source: https://jekyllrb.com/docs/variables/

I can confirm it gives me different results than a list of 10 most recent posts, but still don't think it is perfect. If you try the --lsi option, be sure to install gsl as mention on http://footle.org/2014/11/06/speeding-up-jekylls-lsi/ or else you will be sitting for hours without any progress.

@mmistakes
Copy link
Owner

mmistakes commented Dec 31, 2016

I've always used LSI but pretty sure it's disabled on sites hosted with GitHub Pages. The best solution ice found is using a related post plugin that improves on the one built into Jekyll. But that's sadly not an option for the vast amount of people hosting the theme on GH.

@mmistakes
Copy link
Owner

I came up with a different solution that uses a post tags to compare against and show related posts that way. It can be tested from PR #978 or by copying these two files to your _layouts and _includes folder.

kkunapuli pushed a commit to kkunapuli/kkunapuli.github.io that referenced this issue May 30, 2019
`site.related_posts` should return an array of latest posts when no related ones are found due to `lsi` being disabled (GitHub Page's doesn't support it).

This works around that bug by showing the 4 latest posts to populate the "You May Also Enjoy" module when enabled with `related: true`.

Fixes mmistakes#554
Related: mmistakes#978 mmistakes#1022
adixchen pushed a commit to adixchen/adrianmatei.com that referenced this issue Mar 21, 2020
@dadatuputi
Copy link

It looks like this was closed without ever implementing tag-based related posts - was that intentional? I'm dealing with this now, and it seems that it would be pretty trivial to do, but I'm not sure why you didn't. If this is something that can be rolled into the gem, then I won't bother now, otherwise I'll override it.

@mmistakes
Copy link
Owner

My pull request made build times worse. That is why it wasn’t merged. I wasn’t comfortable degrading build times for users... especially those with a lot of posts.

@dadatuputi
Copy link

Would putting it as a configurable option, for example, related_posts_tag, eliminate that penalty?

@mmistakes
Copy link
Owner

I've been against adding more configuration flags as they have many costs. In this case I don't think it's warranted as you can override the two files above as I have in the PR if you're OK with slower builds.

makaroniame added a commit to makaroniame/makaroniame-old.github.io that referenced this issue May 18, 2022
`site.related_posts` should return an array of latest posts when no related ones are found due to `lsi` being disabled (GitHub Page's doesn't support it).

This works around that bug by showing the 4 latest posts to populate the "You May Also Enjoy" module when enabled with `related: true`.

Fixes mmistakes#554
Related: mmistakes#978 mmistakes#1022
jchwenger pushed a commit to jchwenger/jchwenger.github.io that referenced this issue May 5, 2023
`site.related_posts` should return an array of latest posts when no related ones are found due to `lsi` being disabled (GitHub Page's doesn't support it).

This works around that bug by showing the 4 latest posts to populate the "You May Also Enjoy" module when enabled with `related: true`.

Fixes mmistakes#554
Related: mmistakes#978 mmistakes#1022
koyumi0601 pushed a commit to koyumi0601/koyumi0601.github.io that referenced this issue Jul 31, 2023
Co-authored-by: Matt Wang <matt@matthewwang.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants