From 98cee2f18b0a0c78c5cede60da0c4e8cd578d32e Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 16 Apr 2023 00:57:50 +0800 Subject: [PATCH] body class --- helper/number/number.go | 5 +++ internal/pkg/dao/common.go | 4 +- internal/pkg/dao/posts.go | 5 ++- internal/pkg/models/wp_posts.go | 2 + internal/theme/templateFuncs.go | 9 ++++ .../theme/twentyfifteen/layout/base.gohtml | 2 +- .../theme/twentyfifteen/posts/detail.gohtml | 3 +- .../theme/twentyfifteen/posts/index.gohtml | 3 +- .../theme/wp/{bodyclass.go => calclass.go} | 42 +++++++++++++++++++ internal/theme/wp/wp.go | 7 ++++ internal/wpconfig/term.go | 35 ++++++++++++---- 11 files changed, 101 insertions(+), 16 deletions(-) rename internal/theme/wp/{bodyclass.go => calclass.go} (64%) diff --git a/helper/number/number.go b/helper/number/number.go index a1c5a9e..40bbeee 100644 --- a/helper/number/number.go +++ b/helper/number/number.go @@ -7,6 +7,7 @@ import ( "golang.org/x/exp/constraints" "math" "math/rand" + "strconv" ) func Range[T constraints.Integer](start, end, step T) []T { @@ -73,6 +74,10 @@ func ToString[T constraints.Integer | constraints.Float](n T) string { return fmt.Sprintf("%v", n) } +func IntToString[T constraints.Integer](i T) string { + return strconv.FormatInt(int64(i), 10) +} + func Abs[T constraints.Integer | constraints.Float](n T) T { if n >= 0 { return n diff --git a/internal/pkg/dao/common.go b/internal/pkg/dao/common.go index 570fdc3..a34d69b 100644 --- a/internal/pkg/dao/common.go +++ b/internal/pkg/dao/common.go @@ -57,10 +57,10 @@ func CategoriesAndTags(a ...any) (terms []models.TermsMy, err error) { model.In(in), )) for i := 0; i < len(terms); i++ { - if v, ok := wpconfig.Terms.Load(terms[i].Terms.TermId); ok { + if v, ok := wpconfig.GetTerm(terms[i].Terms.TermId); ok { terms[i].Terms = v } - if v, ok := wpconfig.TermTaxonomies.Load(terms[i].Terms.TermId); ok { + if v, ok := wpconfig.GetTermTaxonomy(terms[i].Terms.TermId); ok { terms[i].TermTaxonomy = v } } diff --git a/internal/pkg/dao/posts.go b/internal/pkg/dao/posts.go index 120e7c3..e35f33e 100644 --- a/internal/pkg/dao/posts.go +++ b/internal/pkg/dao/posts.go @@ -24,7 +24,7 @@ func GetPostsByIds(a ...any) (m map[uint64]models.Posts, err error) { {"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id"}, {"left join", "wp_terms d", "c.term_id=d.term_id"}, }), - model.Fields("a.*,ifnull(d.name,'') category_name,ifnull(taxonomy,'') `taxonomy`"), + model.Fields("a.*,ifnull(d.name,'') category_name,ifnull(c.term_id,0) terms_id,ifnull(taxonomy,'') `taxonomy`"), model.In(slice.ToAnySlice(ids)), )) @@ -42,6 +42,9 @@ func GetPostsByIds(a ...any) (m map[uint64]models.Posts, err error) { } else if post.Taxonomy == "post_tag" { v.Tags = append(v.Tags, post.CategoryName) } + if post.TermsId > 0 { + v.TermIds = append(v.TermIds, post.TermsId) + } postsMap[post.Id] = v } //host, _ := wpconfig.Options.Load("siteurl") diff --git a/internal/pkg/models/wp_posts.go b/internal/pkg/models/wp_posts.go index bab8aa2..06afb68 100644 --- a/internal/pkg/models/wp_posts.go +++ b/internal/pkg/models/wp_posts.go @@ -29,6 +29,8 @@ type Posts struct { CommentCount int64 `gorm:"column:comment_count" db:"comment_count" json:"comment_count" form:"comment_count"` //扩展字段 + TermsId uint64 `db:"terms_id" json:"terms_id"` + TermIds []uint64 `db:"term_ids" json:"term_ids"` Taxonomy string `db:"taxonomy" json:"taxonomy"` CategoryName string `db:"category_name" json:"category_name"` Categories []string `json:"categories"` diff --git a/internal/theme/templateFuncs.go b/internal/theme/templateFuncs.go index ab788d0..0b5ce9c 100644 --- a/internal/theme/templateFuncs.go +++ b/internal/theme/templateFuncs.go @@ -1,6 +1,7 @@ package theme import ( + "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/wpconfig" "html/template" "time" @@ -20,6 +21,14 @@ var comFn = template.FuncMap{ return wpconfig.GetOption(k) }, "getLang": wpconfig.GetLang, + "postsFn": postsFn, + "exec": func(fn func() string) string { + return fn() + }, +} + +func postsFn(fn func(models.Posts) string, a models.Posts) string { + return fn(a) } func FuncMap() template.FuncMap { diff --git a/internal/theme/twentyfifteen/layout/base.gohtml b/internal/theme/twentyfifteen/layout/base.gohtml index 53be8fd..9fbca4f 100644 --- a/internal/theme/twentyfifteen/layout/base.gohtml +++ b/internal/theme/twentyfifteen/layout/base.gohtml @@ -3,7 +3,7 @@ {{template "layout/head" .}} - + {{template "svg"}}