package plugins import ( "fmt" str "github.com/fthvgb1/wp-go/helper/strings" "regexp" "strings" ) type PageEle struct { PrevEle string NextEle string DotsEle string MiddleEle string CurrentEle string } func TwentyFifteenPagination() PageEle { return twentyFifteen } var twentyFifteen = PageEle{ PrevEle: ``, NextEle: ``, DotsEle: ``, MiddleEle: `%d `, CurrentEle: ` %d`, } func (p PageEle) Current(page, totalPage int) string { return fmt.Sprintf(p.CurrentEle, page) } func (p PageEle) Prev(url string) string { return fmt.Sprintf(p.PrevEle, url) } func (p PageEle) Next(url string) string { return fmt.Sprintf(p.NextEle, url) } func (p PageEle) Dots() string { return p.DotsEle } func (p PageEle) Middle(page int, url string) string { return fmt.Sprintf(p.MiddleEle, url, page) } var reg = regexp.MustCompile(`(/page)/(\d+)`) func (p PageEle) Url(path, query string, page int) string { if !strings.Contains(path, "/page/") { path = fmt.Sprintf("%s%s", path, "/page/1") } if page == 1 { path = reg.ReplaceAllString(path, "") } else { s := fmt.Sprintf("$1/%d", page) path = reg.ReplaceAllString(path, s) } path = strings.Replace(path, "//", "/", -1) if path == "" { path = "/" } return str.Join(path, query) }