Преглед изворни кода

Merge remote-tracking branch 'gogs/master'

igb пре 2 недеља
родитељ
комит
845ad6753f

+ 24 - 12
app/Models/SitePageTag.php

@@ -10,21 +10,33 @@ class SitePageTag extends Model
     use HasFactory;
     protected $table = 'site_pages_tag';
 
-    public function pages($perPage = 10)
+    public function pages($perPage = 10,$orderBy = null)
     {
-        return $this->belongsToMany(
-            SitePage::class,
-            'site_pages_tag_relationship',
-            'tag_id',
-            'pages_id'
-        )->where('status', 1)
-            ->orderBy('post_date', 'desc')
-            ->orderBy('id', 'desc')
-            ->paginate($perPage); // 支持分页
+        if ($orderBy != null) {
+            $orderBy = explode(',', $orderBy);
+            return $this->belongsToMany(
+                SitePage::class,
+                'site_pages_tag_relationship',
+                'tag_id',
+                'pages_id'
+            )->where('status', 1)
+                ->orderBy($orderBy[0], $orderBy[1])
+                ->paginate($perPage); // 支持分页
+        } else {
+            return $this->belongsToMany(
+                SitePage::class,
+                'site_pages_tag_relationship',
+                'tag_id',
+                'pages_id'
+            )->where('status', 1)
+                ->orderBy('post_date', 'desc')
+                ->orderBy('id', 'desc')
+                ->paginate($perPage); // 支持分页
+        }
     }
 
     // 根据slug获取标签和关联的页面
-    public static function getBySlug($slug, $perPage = 10)
+    public static function getBySlug($slug, $perPage = 10,$orderBy = null)
     {
         $tag = self::where('slug', $slug)->where('dist_id', getDistId())->first();
 
@@ -33,6 +45,6 @@ class SitePageTag extends Model
         }
 
         // 获取与标签关联的页面
-        return $tag->pages($perPage);
+        return $tag->pages($perPage,$orderBy);
     }
 }

+ 7 - 1
app/Services/LiquidTags/LiquidTagCollection.php

@@ -12,6 +12,8 @@ class LiquidTagCollection extends AbstractBlock
     private $limit;
     private $templateFile;
 
+    private $orderBy;
+
     // 构造函数解析传入的参数
     public function __construct($markup, array &$tokens, $file_system = null)
     {
@@ -19,6 +21,7 @@ class LiquidTagCollection extends AbstractBlock
         $this->tagSlug = null;
         $this->limit = 10; // 默认显示 10 篇
         $this->templateFile = null;
+        $this->orderBy = null; // title,asc 默认按照文章标题排序
 
         // 正则表达式解析传入的参数
         $syntax = '/(\w+)=("[^"]*"|\'[^\']*\'|\d+)/';
@@ -38,6 +41,9 @@ class LiquidTagCollection extends AbstractBlock
                     case 'template':
                         $this->templateFile = $value; // 设置模板文件
                         break;
+                    case 'orderBy':
+                        $this->orderBy = $value; // 设置模板文件
+                        break;
                 }
             }
         }
@@ -59,7 +65,7 @@ class LiquidTagCollection extends AbstractBlock
     private function renderTagPages()
     {
         // 调用 SitePageTag 模型的 getBySlug 方法来获取与标签关联的页面
-        $pages = SitePageTag::getBySlug($this->tagSlug, $this->limit);
+        $pages = SitePageTag::getBySlug($this->tagSlug, $this->limit,$this->orderBy);
 
 
         if (!$pages) {

+ 1 - 1
resources/views/sitemap/index.blade.php

@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8"?>
+<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
     <!-- 首页 -->
     <url>