优化seo文章标题显示;

This commit is contained in:
2022-05-26 10:41:29 +08:00
parent 1d9377da5f
commit 2a71b4e65d
2 changed files with 18 additions and 5 deletions

View File

@@ -28,6 +28,8 @@ class Index extends Common
return $content; return $content;
} }
$page_title = '全部文章';
// //
$sub_category = []; $sub_category = [];
$current_category = []; $current_category = [];
@@ -35,8 +37,11 @@ class Index extends Common
$category_id = $this->request->param('category_id'); $category_id = $this->request->param('category_id');
if (!empty($category_id)) { if (!empty($category_id)) {
$sub_category = Category::where('pid', $category_id)->where('type', 3)->order('sort asc')->select();
$current_category = Category::find($category_id); $current_category = Category::find($category_id);
$page_title = $current_category->title;
$sub_category = Category::where('pid', $category_id)->where('type', 3)->order('sort asc')->select();
if (empty($this->request->param('sub_category_id'))) { if (empty($this->request->param('sub_category_id'))) {
$categorys = [$category_id]; $categorys = [$category_id];
@@ -46,6 +51,7 @@ class Index extends Common
$model_post = Post::hasWhere('categorys', $categorys_where)->where('status', 1)->order('id desc'); $model_post = Post::hasWhere('categorys', $categorys_where)->where('status', 1)->order('id desc');
} else { } else {
$page_title .= "|{$sub_category->title}";
$model_post = Post::hasWhere('categorys', ['category_id' => $this->request->param('sub_category_id')])->where('status', 1)->order('id desc'); $model_post = Post::hasWhere('categorys', ['category_id' => $this->request->param('sub_category_id')])->where('status', 1)->order('id desc');
} }
} else { } else {
@@ -58,16 +64,22 @@ class Index extends Common
$keywords = $this->request->param('keywords'); $keywords = $this->request->param('keywords');
if (!empty($keywords)) { if (!empty($keywords)) {
$model_post->whereLike('title|desc', "%$keywords%"); $model_post->whereLike('title|desc|content', "%$keywords%");
$page_title .= "|{$keywords}";
} }
$page = $this->request->param('page', 1);
$page_title .= "|第{$page}";
$list_post = $model_post->paginate([ $list_post = $model_post->paginate([
'url' => 'Index/index', 'url' => 'Index/index',
'query' => [ 'query' => [
'category_id' => $this->request->param('category_id'), 'category_id' => $this->request->param('category_id'),
'sub_category_id' => $this->request->param('sub_category_id'), 'sub_category_id' => $this->request->param('sub_category_id'),
'page' => $this->request->param('page'), 'page' => $page,
'keywords' => $keywords
], ],
'list_rows' => 10 'list_rows' => 10
]); ]);
@@ -76,6 +88,7 @@ class Index extends Common
View::assign('sub_category', $sub_category); View::assign('sub_category', $sub_category);
View::assign('list_post', $list_post); View::assign('list_post', $list_post);
View::assign('page_title', $page_title);
$content = View::fetch(); $content = View::fetch();

View File

@@ -4,9 +4,9 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$current_category.title|default='全部文章'}|{:get_system_config('site_name')}</title> <title>{$page_title}|{:get_system_config('site_name')}</title>
<meta name="keywords" content="{:get_system_config('site_keywords')}"> <meta name="keywords" content="{:get_system_config('site_keywords')}">
<meta name="description" content="{:get_system_config('site_desc')}{$current_category.title|default='全部文章'},{$current_category.desc|default=''}"> <meta name="description" content="{:get_system_config('site_desc')}{$page_title}{$current_category.desc|default=''}">
{include file='common/_require'/} {include file='common/_require'/}
</head> </head>