How to use hexo related popular posts plugin

本文介绍了如何安装和使用hexo的related popular posts插件,包括在PUG和EJS模板文件中的使用方法,以及遇到问题时的解决策略。此外,还详细说明了如何自定义插件代码,以便在文章中显示图片并美化样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

See the original article https://dyingdown.github.io/2020/08/07/How-to-use-hexo-related-popular-posts/

Install

The most convenient way is to use npm to install.

npm install hexo-related-popular-posts --save

Or you can click here to download zip. https://github.com/tea3/hexo-related-popular-posts

Usage

First, add the following popular_posts( {} , post ) helper tag in template file for article.

If you are using PUG, you can use it this way:

div!= popular_posts( {} , post )

If you are using EJS, you can use it this way:

  <%-
    popular_posts( {} , post )
  %>

Since I don’t know the grammar of swig you can search it yourself or refer the code in Next Theme

Switch

optiondescriptiondefault
maxCountMaximum count of a list5
ulClassClass name of element'popular-posts'
PPMixingRateMixing ratio of popular posts and related posts0.0(=Related posts only)
isDatevisible the datefalse
isImagevisible the imagefalse
isExcerptvisible the excerptfalse
PPCategoryFilterOption to fix category on Popular Postsundefined
PPTagFilterOption to fix tag on Popular Postsundefined

To use these switches, you can type them in {}

popular_posts( {maxCount = 5, ulClass="popular-posts", dsDate = true} , post )

However, if you are using my hexo theme, you just need to set up the _config.yml in the theme directory.

Problem

When I’m using this plugin, I ran into an error say:

Unhandled rejection TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received null

Here are the solution. https://dyingdown.github.io/2020/08/07/The-path-argument-must-be-of-type-string-Hexo/

Custom

The reason why I need to custom the plugin code is that I want it to show pictures when my isImage tag is on. And I want it to looks beautiful and neat. So I need to set up a default picture for the article.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vRCzOhDI-1596775885011)(https://s1.ax1x.com/2020/08/07/aWrKc8.png)]

These pictures are default pictures.

So, to make the style more beautiful, flow these steps.

  1. Find the folder hexo-related-popular-posts in node_modules

  2. Open lib/collector.js

  3. Go to line 54 and insert a line of code:

    eyeCatchImage = "https://s1.ax1x.com/2020/04/25/J6iz9K.jpg"
    

    The value after the = is the link of your image. You can change it to your own favorite picture. I upload my image to a imgchr so I got the url.

  4. Annotate lines from 55 to 63. Like this:

    eyeCatchImage = "https://s1.ax1x.com/2020/04/25/J6iz9K.jpg"
    // let $ = cheerio.load(post.content)
    // if( $("img") && $("img").length > 0){
    //     $("img").each(function(i){
    //       if( i == 0 ){
    //         let imgsrc = $(this).attr("src")
    //         eyeCatchImage = imgsrc
    //       }
    //     })
    // }
    

All done. Refresh your sever.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值