我有用cakephp制作的网站.它有3个级别主页,部分,文章我已经为每个部分和每篇文章分隔了元关键字和描述.
我把这样的主页的元描述
<? if($title_for_layout=="Home"){?>
<title><?php echo $this->Session->read('Setting.title');?></title>
<?
echo $this->Html->meta('keywords', $this->Session->read('Setting.meta_keywords'));
echo $this->Html->meta('description', $this->Session->read('Setting.meta_description'));
?>
<? if($title_for_layout=="Section"){?>
<title><?php echo strip_tags(trim($title_for_layout));?></title>
<?
echo "sawy";
echo $this->Html->meta('keywords', $metaKeywords);
echo $this->Html->meta('description', $metaDescription);
?>
<? } ?>
<? }else{ ?>
<title><?php echo strip_tags(trim($title_for_layout));?></title>
<?
echo $this->Html->meta('keywords', $metaKeywords.' ,'.$article['Section']['meta_keyword'].' ,'.$this->Session->read('Setting.meta_keywords'));
echo $this->Html->meta('description', $metaDescription.' ,'.$article['Section']['meta_description'].' ,'.$this->Session->read('Setting.meta_description'));
?>
<?}?>
Run Code Online (Sandbox Code Playgroud)
我想告诉代码,如果它是部分放置其关键字和如果文章把它的关键字.因为它在这里不起作用的网址示例
domain.com/section/2/programinig domain.com/article/9992/learnphp
对于Cake 2.x
我不会使用布局的标题来决定必须使用哪些元和关键字.
我的建议是使用blocks See documentation.
所以在你的布局文件中(可能default.ctp)我会在标题中加入一些这样的行:
echo $this->Html->meta('keywords', $this->fetch( 'head_keywords' ) );
echo $this->Html->meta('description', $this->fetch( 'head_description' ) );
Run Code Online (Sandbox Code Playgroud)
然后在您的视图文件中,特定于主页,部分或任何您需要的文件,您只需定义这些块.
例如,在主页视图文件中,您将拥有:
$this->assign( 'head_description', 'this is the description for the home page' );
$this->assign( 'head_keywords', 'this are the keywords for the home page' );
Run Code Online (Sandbox Code Playgroud)
您将对截面视图或文章视图执行相同操作.
对于Cake 1.3
CakePHP 1.3没有块,所以你必须像这样模拟它.
在您的布局文件中添加以下代码:
if( !isset($head_keywords) ){ $head_keywords = '';} //define if not defined
if( !isset($head_description) ){ $head_description =''; }
echo $this->Html->meta('keywords', $head_keywords );
echo $this->Html->meta('description', $head_description );
Run Code Online (Sandbox Code Playgroud)
然后在View文件中,您需要以这种方式添加关键字和元数据:
$head_keywords = 'put here your keywords';
$head_description = 'put here your description';
$this->set( compact( 'head_keywords', 'head_description' ) ); //make these visible in the layout context
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2466 次 |
| 最近记录: |