简体   繁体   中英

How to add particular title and description to categories in Laravel

I am trying to add particular title and meta description for every post and category in my Laravel based website.

So far, I've done the title and description for the posts, but I couldn't figure out how to do it for categories too.

This is the code I used for the posts and it worked:

@if(isset($post->title))
  <title>{{ $post->title }}</title>
@else
  <title>{{ $settings->website_name }} - {{ $settings->website_description }}</title>
@endif

@if(isset($post->description))
<meta name="description" content="{{ $post->description }}" />
@else
<meta name="description" content="Example Example Example" />
@endif

I tried to do something similar to this for categories too:

@if(isset($categories->name) && ($categories->description))
<title>{{ categories->name }}</title>
<meta name="description" content="{{ categories->description }}">
@else
<title>Whatever</title>
<meta name="description" content="Whatever">
@endif

In this code, "categories" is the name of my categories table, while "name" and "description" are it's columns.

This is what I have on error_log:

#0 /home/example/public_html/example/app/storage/views

/cba078e6a3a6e27c09d6787a6da4672d(22): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', '/home/example/...', 22, Array)
#1 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(38): include('/home/example/...')
#2 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(45): Illuminate\View\Engines\PhpEngine->evaluatePath('/home/example/...', Array)
#3 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/View.php(100): Illuminate\View\Engines\CompilerEngine->get('/home/example/...', Array)
#4 /home/example/public_html/fun/vendor/laravel/framework/src/Illuminate/View/View.php(81): Illuminate\View\View->getContents()
#5 /home/example/public_html/fu in /home/gametops/public_html/fun/app/storage/views/cba078e6a3a6e27c09d6787a6da4672d on line 22

This is how I get the categories:

<a href="#" class="dropdown-toggle categories" data-toggle="dropdown"><i class="fa fa-folder-open"></i> {{ Lang::get('category.categories') }} <b class="caret"></b><div class="nav-border-bottom"></div></a>

<ul class="dropdown-menu">
                <li>
                    @foreach ($categories as $category)
                      <a href="{{ URL::to('category') . '/' . slugify($category->name) }}">{{ $category->name }}</a>
                    @endforeach
                </li>
              </ul>

Hi if you are using blade then is simple just make is simple if you want to put unique title and description then modifiy blade layout like this

<title>@yield('meta_title', 'Default Title')</title>
<meta name="description" content="@yield('meta_description', 'Default Description')" />

now in your any views you can call like this

@section('meta_title')
    Your title text will go here
@stop

@section('meta_description')
    Your description text will go here
@stop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM