Closed s-moon closed 2 years ago
What do you mean refer to the posts actual title instead, rather than url
? Could you give an example?
Hi Sergey,
Thank you for your prompt response and sorry, I should have pasted in an example. Here's one of my blog posts with the links at the bottom:
What I would rather have is to have the post's title as the display text, instead of the actual URL. So taking the first example, it would look like this:
It feels like that information isn't available but I am fairly new to Hexo so I am not certain. Is it possible somehow?
Thanks,
Just trying out this plugin and had the same issue. Here is a solution I found:
<% if (page.related_posts && page.related_posts.length > 0) { %>
<section class="related-posts">
<h2>Related posts</h2>
<ul>
<%
for (const path of page.related_posts) {
var posts = site.posts.filter(function(post) {
return post.path === path;
});
if (posts && posts.length > 0) {
posts.each(function(apost) {
%>
<li><a href="<%- url_for(apost.path) %>"><%= apost.title || '(no title)' %></a></li>
<%
});
}
}
%>
</ul>
</section>
<% } %>
Got the idea from here.
It would've been nice if the plugin had something like this built in. But if it's not possible, I think at least this code should be included in the readme.
@s-moon @adnan360 In my templates I use this template:
<% if (posts && posts.length > 0) { %>
<section class="post__related">
<h2 class="post__related-title"><%= __('post.related') %></h2>
<ul class="post__related-list">
<% for (const path of posts) { %> <% const url = post_url(path) %> <% const title = post_title(path) %> <% if (url && title) { %>
<li class="post__related-item"><a href="<%= url %>" class="post__related-link"><%= title %></a></li>
<% } %> <% } %>
</ul>
</section>
<% } %>
and somewhere in helpers:
const { magenta } = require('chalk');
hexo.extend.helper.register('resolve_post', (p) => {
const { log } = hexo;
const post = hexo.locals.get('posts').data.find(({ slug, path }) => (slug === p) || (path === p));
if (post) {
return post;
} else {
log.warn('Unable to resolve %s post', magenta(p));
}
});
// ...
hexo.extend.helper.register('post_title', (path) => {
const resolve_post = hexo.extend.helper.get('resolve_post').bind(hexo);
const post = resolve_post(path);
if (post) {
return post.title;
}
});
This is how it's actually looks like:
https://blog.zwezdin.com/2017/asp-net-core-logging-into-kibana/
Could you please create the PR if you want to update readme.
@sergeyzwezdin Your code seems like a good way to do it. But I guess my code is ok for average people and it requires editing only 1 file. So I made a PR (#7) with my code. Feel free to let me know if any change is necessary.
@adnan360 thanks!
Thank you for this package - it's great and does what I am looking for.
I have a question regarding the example output though, please. In the suggested post code you showed this:
<li><a href="<%= url %>"><%= url %></a></li>
which uses the URL as the A tags text. Is it possible to refer to the posts actual title instead, rather than url?
Thanks