body class
This commit is contained in:
parent
b53819f733
commit
98cee2f18b
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<html lang="{{getLang}}" class="no-js">
|
||||
{{template "layout/head" .}}
|
||||
|
||||
<body class="{{.bodyClass}}">
|
||||
<body class="{{.calBodyClass|exec}}">
|
||||
{{template "svg"}}
|
||||
<div id="page" class="hfeed site">
|
||||
<a class="skip-link screen-reader-text" href="#content">
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
{{ if and (.post) (gt .post.Id 0)}}
|
||||
<div id="primary" class="content-area">
|
||||
<main id="main" class="site-main">
|
||||
<article id="post-{{.post.Id}}"
|
||||
class="post-{{.post.Id}} post type-post status-publish format-standard hentry category-uncategorized">
|
||||
<article class="{{ .post|postsFn .calPostClass}}">
|
||||
|
||||
<header class="entry-header">
|
||||
<h1 class="entry-title">{{.post.PostTitle}}</h1></header><!-- .entry-header -->
|
||||
|
|
|
@ -12,8 +12,7 @@
|
|||
</header>
|
||||
{{end}}
|
||||
{{ range $k,$v:=.posts}}
|
||||
<article id="post-{{$v.Id}}"
|
||||
class="post-{{$v.Id}} post {{if $v.IsSticky}}sticky{{end}} {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category">
|
||||
<article class="{{ $v|postsFn $.calPostClass}}">
|
||||
{{if $v.Thumbnail.Path}}
|
||||
<a class="post-thumbnail" href="/p/{{$v.Id}}" aria-hidden="true">
|
||||
<img width="{{$v.Thumbnail.Width}}" height="{{$v.Thumbnail.Height}}" src="{{$v.Thumbnail.Path}}" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="{{$v.PostTitle}}" decoding="async">
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package wp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fthvgb1/wp-go/helper/number"
|
||||
"github.com/fthvgb1/wp-go/helper/slice"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
|
@ -75,3 +76,44 @@ func (h *Handle) BodyClass() string {
|
|||
}
|
||||
return h.ComponentFilterFnHook("bodyClass", strings.Join(class, " "))
|
||||
}
|
||||
func (h *Handle) PostClass(posts models.Posts) string {
|
||||
var class []string
|
||||
class = append(class, fmt.Sprintf("post-%d", posts.Id), posts.PostType,
|
||||
str.Join("type-", posts.PostType), str.Join("status-", posts.PostStatus),
|
||||
"hentry", "format-standard")
|
||||
if h.CommonThemeMods().ThemeSupport.PostThumbnails && posts.Thumbnail.Path != "" {
|
||||
class = append(class, "has-post-thumbnail")
|
||||
}
|
||||
|
||||
if posts.PostPassword != "" {
|
||||
if h.password != posts.PostPassword {
|
||||
class = append(class, "post-password-required")
|
||||
} else {
|
||||
class = append(class, "post-password-projected")
|
||||
}
|
||||
}
|
||||
|
||||
if h.scene == constraints.Home && h.IsStick(posts.Id) {
|
||||
class = append(class, "sticky")
|
||||
}
|
||||
for _, id := range posts.TermIds {
|
||||
term, ok := wpconfig.GetTermMy(id)
|
||||
if !ok || term.Slug == "" {
|
||||
continue
|
||||
}
|
||||
termClass := term.Slug
|
||||
if termClass[0] == '%' {
|
||||
termClass = number.ToString(term.Terms.TermId)
|
||||
}
|
||||
switch term.Taxonomy {
|
||||
case "category":
|
||||
class = append(class, str.Join("category-", termClass))
|
||||
case "post_tag":
|
||||
class = append(class, str.Join("tag-", termClass))
|
||||
case "post_format":
|
||||
class = append(class, fmt.Sprintf("format-%s", strings.ReplaceAll(term.Slug, "post-format-", "")))
|
||||
}
|
||||
}
|
||||
|
||||
return h.ComponentFilterFnHook("postClass", strings.Join(class, " "))
|
||||
}
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -84,6 +85,12 @@ func InitThemeArgAndConfig(fn func(*Handle), h *Handle) {
|
|||
}
|
||||
h.components = m
|
||||
h.ginH = maps.Copy(hh.ginH)
|
||||
h.ginH["calPostClass"] = func(posts models.Posts) string {
|
||||
return h.PostClass(posts)
|
||||
}
|
||||
h.ginH["calBodyClass"] = func() string {
|
||||
return h.BodyClass()
|
||||
}
|
||||
if inited {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,30 +1,49 @@
|
|||
package wpconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"github.com/fthvgb1/wp-go/safety"
|
||||
)
|
||||
|
||||
var Terms safety.Map[uint64, models.Terms]
|
||||
var TermTaxonomies safety.Map[uint64, models.TermTaxonomy]
|
||||
var terms = safety.NewMap[uint64, models.Terms]()
|
||||
var termTaxonomies = safety.NewMap[uint64, models.TermTaxonomy]()
|
||||
|
||||
var my = safety.NewMap[uint64, models.TermsMy]()
|
||||
|
||||
func GetTerm(termId uint64) (models.Terms, bool) {
|
||||
return terms.Load(termId)
|
||||
}
|
||||
|
||||
func GetTermTaxonomy(termId uint64) (models.TermTaxonomy, bool) {
|
||||
return termTaxonomies.Load(termId)
|
||||
}
|
||||
func GetTermMy(termId uint64) (models.TermsMy, bool) {
|
||||
return my.Load(termId)
|
||||
}
|
||||
|
||||
func InitTerms() (err error) {
|
||||
ctx := context.Background()
|
||||
terms, err := model.SimpleFind[models.Terms](ctx, nil, "*")
|
||||
terms.Flush()
|
||||
termTaxonomies.Flush()
|
||||
term, err := model.SimpleFind[models.Terms](ctx, nil, "*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, wpTerms := range terms {
|
||||
Terms.Store(wpTerms.TermId, wpTerms)
|
||||
for _, wpTerms := range term {
|
||||
terms.Store(wpTerms.TermId, wpTerms)
|
||||
}
|
||||
termTax, err := model.SimpleFind[models.TermTaxonomy](ctx, nil, "*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, taxonomy := range termTax {
|
||||
TermTaxonomies.Store(taxonomy.TermTaxonomyId, taxonomy)
|
||||
termTaxonomies.Store(taxonomy.TermTaxonomyId, taxonomy)
|
||||
if term, ok := terms.Load(taxonomy.TermId); ok {
|
||||
my.Store(taxonomy.TermId, models.TermsMy{
|
||||
Terms: term,
|
||||
TermTaxonomy: taxonomy,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user