Custom title tag for tags or categories

Hello,
I’d like to know how to create a title tag for tags or categories. Does anyone have a snippet to achieve this:

Title of Category - Site Title

or

Title of Tag - Site Title

For example, a blog should not have the same title repeated. Each category, or tag, should have it’s own title (with SEO in mind).

Thank you :slight_smile:

No one has idea how to do that? :confused:

You can get the tag or category from the URL:

<?php
$tag = param('tag');
e($tag, $tag, '');

You can then use it in your title tag.

The param helper even supports default values:

echo param('tag', '<some default if no tag page>');

Can you show me, please, a complete title in a if-else statements like that:

  <?php if($page->isHomePage()): ?>

    <title><?= html($site->title()) ?></title>

  <?php else: ?>
  
    <?php if(param('category')): ?>

      <title><?= param('category') ?></title>

    <?php else: ?>

      <title><?= html($page->title()) . ' • ' . html($site->title()) ?></title>
    
    <?php endif ?>

  <?php endif ?>

Does it not work as expected? I only added the tag option, so this should work:

<?php if($page->isHomePage()): ?>

    <title><?= $site->title()->html() ?></title>

<?php else: ?>
  
    <?php if($category = param('category')): ?>

      <title><?= $category . ' • ' . $site->title()->html() ?></title>

   <?php elseif($tag = param('tag')): ?>

      <title><?= $tag . ' • ' . $site->title()->html() ?></title>
 
   <?php else: ?>

      <title><?= $page->title() ->html() . ' • ' . $site->title()->html() ?></title>
    
    <?php endif ?>

<?php endif ?>

From this code works (I only use categories, no tags):

  <?php if($category = param('category')): ?>

      <title><?= ucwords($category) . ' • ' . $site->title()->html() ?></title>

  <?php else: ?>

      <title><?= $page->title() ->html() . ' • ' . $site->title()->html() ?></title>

  <?php endif ?>

With the exception of the home page:

<?php if($page->isHomePage()): ?>

    <title><?= $site->title()->html() ?></title>

<?php endif ?>

How can I join all this if-else statements? Thanks :wink:

I must admit I don’t understand the problem. Your code above

<?php if($page->isHomePage()): ?>

    <title><?= html($site->title()) ?></title>

  <?php else: ?>
  
    <?php if(param('category')): ?>

      <title><?= param('category') ?></title>

    <?php else: ?>

      <title><?= html($page->title()) . ' • ' . html($site->title()) ?></title>
    
    <?php endif ?>

  <?php endif ?>

is perfectly alright and should work unless you want to achieve something else.

It renders the site title on the home page and the page + site title on every other page, unless there is a parameter. In that case the parameter is rendered. You can add the site title to the param if you want, but no need to do so.

Simply the category name does not appear on title, see here:
CloudApp

I don’t know why :confused:

I’m using the Baseblog theme, maybe something is wrong.

Yes, but this is on the homepage, but according to the code, you don’t want this behaviour on the homepage. If you only want it on the homepage, you have to change your code like this:

<?php if(! $page->isHomePage()): ?>
  <title><?= $page->title()->html() . ' • ' . $site->title()->html() ?></title>
<?php else: ?>
  
    <?php if($category = param('category')): ?>

      <title><?= $category . ' • ' . $site->title()->html() ?></title>

    <?php else: ?>

      <title><?= $page->title()->html() . ' • ' . $site->title()->html() ?></title>
    
    <?php endif ?>

  <?php endif ?>

Otherwise, please let me know what exactly you want to achieve.

1 Like

Yes, now works perfectly the code, thanks Sonja :slight_smile: