diff --git a/internal/pkg/cache/headerImages.go b/internal/pkg/cache/headerImages.go index 7d617e9..fd8ff00 100644 --- a/internal/pkg/cache/headerImages.go +++ b/internal/pkg/cache/headerImages.go @@ -22,20 +22,26 @@ func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) { theme := a[1].(string) mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme)) if ok && mods != "" { - meta, er := plugins.UnPHPSerialize[plugins.HeaderImageMeta](mods) + meta, er := plugins.UnPHPSerialize[plugins.ThemeMods](mods) if er != nil || meta.HeaderImage == "" { err = er return } if "random-uploaded-image" == meta.HeaderImage { - headers, er := model.Find[models.Posts](ctx, model.SqlBuilder{ - {"post_type", "attachment"}, - {"post_status", "inherit"}, - {"meta_value", theme}, - {"meta_key", "_wp_attachment_is_custom_header"}, - }, "a.ID", "a.ID", nil, model.SqlBuilder{ - {" a", "left join", "wp_postmeta b", "a.ID=b.post_id"}, - }, nil, 0) + headers, er := model.Finds[models.Posts](ctx, model.Conditions( + model.Where(model.SqlBuilder{ + {"post_type", "attachment"}, + {"post_status", "inherit"}, + {"meta_value", theme}, + {"meta_key", "_wp_attachment_is_custom_header"}, + }), + model.Fields("a.ID"), + model.Group("a.ID"), + model.Join(model.SqlBuilder{ + {" a", "left join", "wp_postmeta b", "a.ID=b.post_id"}, + }), + )) + if er != nil { err = er return diff --git a/internal/plugins/thumbnail.go b/internal/plugins/thememods.go similarity index 69% rename from internal/plugins/thumbnail.go rename to internal/plugins/thememods.go index 6046fbf..5182f60 100644 --- a/internal/plugins/thumbnail.go +++ b/internal/plugins/thememods.go @@ -7,12 +7,20 @@ import ( "strings" ) -type HeaderImageMeta struct { - CustomCssPostId int `json:"custom_css_post_id,omitempty"` - NavMenuLocations []string `json:"nav_menu_locations,omitempty"` - HeaderImage string `json:"header_image,omitempty"` - HeaderImagData ImageData `json:"header_image_data,omitempty"` - SidebarsWidgets Sidebars `json:"sidebars_widgets"` +type ThemeMods struct { + CustomCssPostId int `json:"custom_css_post_id,omitempty"` + NavMenuLocations []string `json:"nav_menu_locations,omitempty"` + HeaderImage string `json:"header_image,omitempty"` + BackgroundImage string `json:"background_image"` + BackgroundSize string `json:"background_size"` + BackgroundRepeat string `json:"background_repeat"` + BackgroundColor string `json:"background_color"` + ColorScheme string `json:"color_scheme"` + SidebarTextcolor string `json:"sidebar_textcolor"` + HeaderBackgroundColor string `json:"header_background_color"` + HeaderTextcolor string `json:"header_textcolor"` + HeaderImagData ImageData `json:"header_image_data,omitempty"` + SidebarsWidgets Sidebars `json:"sidebars_widgets"` } type Sidebars struct { diff --git a/internal/theme/twentyfifteen/twentyfifteen.go b/internal/theme/twentyfifteen/twentyfifteen.go index 00fa1f0..95e4586 100644 --- a/internal/theme/twentyfifteen/twentyfifteen.go +++ b/internal/theme/twentyfifteen/twentyfifteen.go @@ -1,9 +1,15 @@ package twentyfifteen import ( + "fmt" + "github.com/fthvgb1/wp-go/helper/slice" + "github.com/fthvgb1/wp-go/internal/pkg/cache" "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/plugins" "github.com/fthvgb1/wp-go/internal/theme/common" + "github.com/gin-gonic/gin" "net/http" ) @@ -37,7 +43,8 @@ func Hook(h *common.Handle) { func (i *indexHandle) Index() { i.Templ = "twentyfifteen/posts/index.gohtml" - + img := getHeaderImage(i.C) + fmt.Println(img) err := i.BuildIndexData(common.NewIndexParams(i.C)) if err != nil { i.Stats = constraints.Error404 @@ -66,3 +73,18 @@ func (d *detailHandle) Detail() { d.RenderComment() d.C.HTML(d.Code, d.Templ, d.GinH) } + +func getHeaderImage(c *gin.Context) (r models.PostThumbnail) { + r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg" + r.Width = 2000 + r.Height = 1200 + hs, err := cache.GetHeaderImages(c, ThemeName) + if err != nil { + logs.ErrPrintln(err, "获取页眉背景图失败") + } else if len(hs) > 0 && err == nil { + _, r = slice.Rand(hs) + + } + r.Sizes = "100vw" + return +}