In the previous section, we introduced the display of the list of articles and the implementation of the filtering function of articles according to classification. In this section we will look at ways to get a list of adjacent associated articles by article classification.
Often, we put some relevant articles on the right side of the details of the article, such as the latest published articles, articles related to this article and other information display. We now place a list of recent and related articles to the right of our article.
HTML code for the latest and related articles
Open the template/article/detail. HTML, on the right code to add the code of the display of the latest articles, article:
<div class="layui-col-md4">
{% include "partial/author.html" %}
<div class="layui-card">
<div class="layui-card-header">The latest article</div>
<div class="layui-card-body">
<ul class="aside-list">
{% for item in newest %}
<li class="item">
<a href="/article/{{item.Id}}" class="link">
<h5 class="title">{{item.Title}}</h5>
<span class="extra">{{stampToDate(article.CreatedTime, "2006-01-02")}}</span>
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
<div class="layui-card">
<div class="layui-card-header">Related articles</div>
<div class="layui-card-body">
<ul class="aside-list">
{% for item in relationList %}
<li class="item">
<a href="/article/{{item.Id}}" class="link">
<h5 class="title">{{item.Title}}</h5>
<span class="extra">{{item.Views}}reading</span>
</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
Copy the code
Latest post title and post date, used to show when the post was published. Related articles display the number of articles read, because related articles are often older, and then display the date of publication is not appropriate, display the number of articles read better.
Controller code for the latest article and related articles
Next, we add the latest and related article reading code to the ArticleDetail() function in controller/article.go:
// The latest article
newest, _, _ := provider.GetArticleList(article.CategoryId, "id desc".1.10)
// Adjacent related articles
relationList, _ := provider.GetRelationArticleList(article.CategoryId, id, 10)
Copy the code
The latest article uses the article list reading function, so we don’t need to write any extra logic, just call provider.getarticlelist (). Of course, we need to get the most recent articles, and the published articles id is increasing, so we can set the sorting rule by ID desc to get the most recent articles list.
Next to the list of related articles, we need to re-implement a function to handle it. In provider/article.go, add the GetRelationArticleList() function:
func GetRelationArticleList(categoryId uint, id uint, limit int) ([]model.Article, error) {
var articles []model.Article
var articles2 []model.Article
db := config.DB
if err := db.Model(model.Article{}).Where("`status` = 1").Where("`id` > ?", id).Where("`category_id` = ?", categoryId).Order("id ASC").Limit(limit/2).Find(&articles).Error; err ! =nil {
//no
}
if err := db.Model(model.Article{}).Where("`status` = 1").Where("`id` < ?", id).Where("`category_id` = ?", categoryId).Order("id DESC").Limit(limit/2).Find(&articles2).Error; err ! =nil {
//no
}
// The list does not return content
if len(articles2) > 0 {
for _, v := range articles2 {
articles = append(articles, v)
}
}
return articles, nil
}
Copy the code
When obtaining relevant articles, we use the classification ID of the current article as the correlation factor of relevant articles. If we add more tags to the article later, we can also use more tags as correlation factors for related articles. Have the same hashtags, same keywords, etc. Here, we only added the classification attribute to the article, so we can only rely on the classification ID to do the association.
In order to reduce the repetition probability of related articles for each article, we split the list of related articles into two parts, one is the closest related articles with a larger id than the current article, and the other is the closest related articles with a smaller ID than the current article. Then combine the two sections and present them as a list of related articles.
The complete project sample code is hosted on GitHub. The complete project code can be viewed at github.com/fesiong/gob… You can also fork a copy to make changes on it.