CakePHP 2.1.3 SEO Meta Descriptions for individual pages vs default.ctp

699 views Asked by At

Currently my meta descriptions and keywords are contained in my default.ctp file located in \app\View\Layouts. It seems that this results in the same meta description for every page on my site. Hopefully this is okay.

I'm adding new pages to my site and would like to be able to customize the meta description and keywords just for these specific pages. So leave the same generic meta description for most of the website, but for a handful or pages, use custom meta descriptions.

For example, is there code that I can add to the individual .ctp files located in app\View\Pages that would then override the meta descriptions found in default.ctp?

Thanks for any and all help!

1

There are 1 answers

2
ndm On

I'm not a SEO expert, so I can't tell you how good or bad duplicate descriptions are with respect to page ranking and stuff, however, from my experience, Google for example will tend to prefer page contents over meta descriptions more often in case the description duplicates on many pages, however there doesn't necessarily have to be correlation of course.

That being said, I'd suggest to use view blocks, they can be overriden from anywhere in your templates.

Layout

<!DOCTYPE html>
<html>
    <head>
        <!-- ... -->
        <?php 
        $this->startIfEmpty('description');
        echo $this->Html->meta('description', 'Default description');
        $this->end();
        echo $this->fetch('description');
        ?>
        <!-- ... -->
    </head>

Update: startIfEmpty() was introduced with CakePHP 2.3, in earlier versions it would be necessary to test for an existing block manually, something like for example

$description = $this->fetch('description');
if(empty($description)) {
    echo $this->Html->meta('description', 'Default description');
}
echo $description;

Specific page template

$this->assign('description', $this->Html->meta('description', 'Specific description'));

See http://book.cakephp.org/2.0/en/views.html#view-blocks for more information.