顶置
This commit is contained in:
parent
e10dc4e45e
commit
54448f68b0
|
@ -35,6 +35,7 @@ type Posts struct {
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
CategoriesHtml string
|
CategoriesHtml string
|
||||||
TagsHtml string
|
TagsHtml string
|
||||||
|
IsSticky bool
|
||||||
Thumbnail PostThumbnail
|
Thumbnail PostThumbnail
|
||||||
AttachmentMetadata WpAttachmentMetadata
|
AttachmentMetadata WpAttachmentMetadata
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,32 +32,35 @@
|
||||||
</div><!-- .entry-content -->
|
</div><!-- .entry-content -->
|
||||||
|
|
||||||
<footer class="entry-footer">
|
<footer class="entry-footer">
|
||||||
<span class="posted-on">
|
{{if $v.IsSticky}}
|
||||||
<span class="screen-reader-text">发布于 </span>
|
<span class="sticky-post">特色</span>
|
||||||
<a href="/p/{{$v.Id}}" rel="bookmark">
|
{{end}}
|
||||||
<time class="entry-date published updated" datetime="{{$v.PostDateGmt}}">{{$v.PostDate|dateCh}}
|
<span class="posted-on">
|
||||||
</time>
|
<span class="screen-reader-text">发布于 </span>
|
||||||
</a>
|
<a href="/p/{{$v.Id}}" rel="bookmark">
|
||||||
</span>
|
<time class="entry-date published updated" datetime="{{$v.PostDateGmt}}">{{$v.PostDate|dateCh}}
|
||||||
|
</time>
|
||||||
|
</a>
|
||||||
|
</span>
|
||||||
{{if $v.CategoriesHtml}}
|
{{if $v.CategoriesHtml}}
|
||||||
<span class="cat-links">
|
<span class="cat-links">
|
||||||
<span class="screen-reader-text">分类 </span>
|
<span class="screen-reader-text">分类 </span>
|
||||||
{{$v.CategoriesHtml|unescaped}}
|
{{$v.CategoriesHtml|unescaped}}
|
||||||
</span>
|
</span>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{if $v.TagsHtml}}
|
{{if $v.TagsHtml}}
|
||||||
<span class="tags-links">
|
<span class="tags-links">
|
||||||
<span class="screen-reader-text">标签 </span>
|
<span class="screen-reader-text">标签 </span>
|
||||||
{{$v.TagsHtml|unescaped}}
|
{{$v.TagsHtml|unescaped}}
|
||||||
</span>
|
</span>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if gt $v.CommentCount 0}}
|
{{if gt $v.CommentCount 0}}
|
||||||
<span class="comments-link">
|
<span class="comments-link">
|
||||||
<a href="/p/{{$v.Id}}#comments">
|
<a href="/p/{{$v.Id}}#comments">
|
||||||
<span class="screen-reader-text">{{$v.PostTitle}}</span>有{{$v.CommentCount}}条评论
|
<span class="screen-reader-text">{{$v.PostTitle}}</span>有{{$v.CommentCount}}条评论
|
||||||
</a>
|
</a>
|
||||||
</span>
|
</span>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
</footer><!-- .entry-footer -->
|
</footer><!-- .entry-footer -->
|
||||||
|
|
|
@ -17,8 +17,12 @@
|
||||||
<div id="primary" class="content-area">
|
<div id="primary" class="content-area">
|
||||||
<main id="main" class="site-main">
|
<main id="main" class="site-main">
|
||||||
{{ range $k,$v:=.posts}}
|
{{ range $k,$v:=.posts}}
|
||||||
<article id="post-{{$v.Id}}" class="post-{{$v.Id}} post {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category">
|
<article id="post-{{$v.Id}}" class="post-{{$v.Id}} post {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} {{if $v.IsSticky}}sticky{{end}} type-post status-publish format-standard hentry category">
|
||||||
|
{{if $v.IsSticky}}
|
||||||
|
<svg class="icon icon-thumb-tack" aria-hidden="true" role="img">
|
||||||
|
<use href="#icon-thumb-tack" xlink:href="#icon-thumb-tack"></use>
|
||||||
|
</svg>
|
||||||
|
{{end}}
|
||||||
<header class="entry-header">
|
<header class="entry-header">
|
||||||
<div class="entry-meta">
|
<div class="entry-meta">
|
||||||
<span class="screen-reader-text">发布于 </span>
|
<span class="screen-reader-text">发布于 </span>
|
||||||
|
|
|
@ -82,6 +82,9 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
|
||||||
case constraints.Home, constraints.Category, constraints.Tag, constraints.Author:
|
case constraints.Home, constraints.Category, constraints.Tag, constraints.Author:
|
||||||
|
|
||||||
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
|
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
|
||||||
|
if i.scene == constraints.Home && i.Param.Page == 1 {
|
||||||
|
i.MarkSticky(&posts)
|
||||||
|
}
|
||||||
|
|
||||||
case constraints.Search:
|
case constraints.Search:
|
||||||
|
|
||||||
|
@ -162,3 +165,13 @@ func Indexs(h *Handle) {
|
||||||
PreCodeAndStats(h)
|
PreCodeAndStats(h)
|
||||||
PreTemplate(h)
|
PreTemplate(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) MarkSticky(posts *[]models.Posts) {
|
||||||
|
a := i.StickPosts()
|
||||||
|
if len(a) < 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*posts = append(a, slice.Filter(*posts, func(post models.Posts, _ int) bool {
|
||||||
|
return !i.IsStick(post.Id)
|
||||||
|
})...)
|
||||||
|
}
|
||||||
|
|
47
internal/theme/wp/stickyposts.go
Normal file
47
internal/theme/wp/stickyposts.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package wp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/elliotchance/phpserialize"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/maps"
|
||||||
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||||
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handle) StickPosts() []models.Posts {
|
||||||
|
return reload.GetAnyValBys("stickPosts", h, func(h *Handle) (r []models.Posts) {
|
||||||
|
v := wpconfig.GetOption("sticky_posts")
|
||||||
|
if v == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
array, err := phpserialize.UnmarshalIndexedArray([]byte(v))
|
||||||
|
if err != nil {
|
||||||
|
logs.ErrPrintln(err, "解析option sticky_posts错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r = slice.FilterAndMap(array, func(t any) (models.Posts, bool) {
|
||||||
|
id := str.ToInt[uint64](fmt.Sprintf("%v", t))
|
||||||
|
post, err := cache.GetPostById(h.C, id)
|
||||||
|
post.IsSticky = true
|
||||||
|
return post, err == nil
|
||||||
|
})
|
||||||
|
return
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) StickMapPosts() map[uint64]models.Posts {
|
||||||
|
return reload.GetAnyValBys("stickPostsMap", h, func(h *Handle) map[uint64]models.Posts {
|
||||||
|
return slice.SimpleToMap(h.StickPosts(), func(v models.Posts) uint64 {
|
||||||
|
return v.Id
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handle) IsStick(id uint64) bool {
|
||||||
|
return maps.IsExists(h.StickMapPosts(), id)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user