发布于
diff --git a/internal/theme/wp/index.go b/internal/theme/wp/index.go
index f791402..3a4133c 100644
--- a/internal/theme/wp/index.go
+++ b/internal/theme/wp/index.go
@@ -82,6 +82,9 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
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)
+ if i.scene == constraints.Home && i.Param.Page == 1 {
+ i.MarkSticky(&posts)
+ }
case constraints.Search:
@@ -162,3 +165,13 @@ func Indexs(h *Handle) {
PreCodeAndStats(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)
+ })...)
+}
diff --git a/internal/theme/wp/stickyposts.go b/internal/theme/wp/stickyposts.go
new file mode 100644
index 0000000..e0476a5
--- /dev/null
+++ b/internal/theme/wp/stickyposts.go
@@ -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)
+}