This commit is contained in:
xing 2022-09-17 09:42:23 +08:00
parent 9a362fc329
commit 46088d1ed6
3 changed files with 25 additions and 3 deletions

View File

@ -18,10 +18,12 @@ var PostsCache sync.Map
func index(c *gin.Context) {
page := 1
pageSize := 10
status := []interface{}{"publish"}
order := c.Query("order")
if !helper.IsContainInArr(order, []string{"asc", "desc"}) {
order = "asc"
}
header := ""
where := models.SqlBuilder{{
"post_type", "post",
}, {"post_status", "in", ""}}
@ -37,6 +39,7 @@ func index(c *gin.Context) {
where = append(where, []string{
"month(post_date)", month,
})
header = fmt.Sprintf("月度归档: <span>%s年%s月</span>", year, month)
}
tt := ""
category := c.Param("category")
@ -44,9 +47,22 @@ func index(c *gin.Context) {
category = c.Param("tag")
if category != "" {
tt = "post_tag"
header = fmt.Sprintf("标签: <span>%s</span>", category)
}
} else {
tt = "category"
header = fmt.Sprintf("分类: <span>%s</span>", category)
}
s := c.Query("s")
if s != "" && strings.Replace(s, " ", "", -1) != "" {
q := helper.StrJoin("%", s, "%")
where = append(where, []string{
"and", "post_title", "like", q, "",
"or", "post_content", "like", q, "",
})
header = fmt.Sprintf("%s的搜索结果", s)
} else {
status = append(status, "private")
}
var join models.SqlBuilder
if category != "" {
@ -70,7 +86,6 @@ func index(c *gin.Context) {
}
}
status := []interface{}{"publish", "private"}
postIds, totalRaw, err := models.SimplePagination[models.WpPosts](where, "ID", "", page, pageSize, models.SqlBuilder{{"post_date", order}}, join, status)
defer func() {
if err != nil {
@ -156,6 +171,8 @@ func index(c *gin.Context) {
"totalPage": totalPage,
"queryRaw": q,
"pagination": pagination(page, totalPage, 1, c.Request.URL.Path, q),
"search": s,
"header": header,
})
}

View File

@ -2,6 +2,11 @@
{{define "content" }}
<div id="primary" class="content-area">
<main id="main" class="site-main">
{{if .header}}
<header class="page-header">
<h1 class="page-title">{{.header|unescaped}}</h1>
</header>
{{end}}
{{ range $k,$v:=.posts}}
<article id="post-{{$v.Id}}"
class="post-{{$v.Id}} post type-post status-publish format-standard hentry category-uncategorized">

View File

@ -1,10 +1,10 @@
{{define "layout/sidebar" }}
<div id="widget-area" class="widget-area" role="complementary">
<aside id="search-2" class="widget widget_search">
<form role="search" method="get" class="search-form" action="https://www.xloyy.com/">
<form role="search" method="get" class="search-form" action="/">
<label>
<span class="screen-reader-text">搜索:</span>
<input type="search" class="search-field" placeholder="搜索…" value="" name="s">
<input type="search" class="search-field" placeholder="搜索…" value="{{.search}}" name="s">
</label>
<input type="submit" class="search-submit screen-reader-text" value="搜索">
</form>