cakephp分隔meta关键字&描述

时间:2014-01-28 11:28:22

标签: cakephp keyword meta-tags

我的网站是用cakephp制作的。它有3个级别主页,部分,文章 我已将meta关键字&每个部分的描述和每篇文章。

我把主页的元描述像这样

<? 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'));
?>
<?}?>

我想告诉代码,如果它是关键字&amp;如果是文章把它的关键字。 因为它不起作用 这里的网址示例

domain.com/section/2/programinig domain.com/article/9992/learnphp

2 个答案:

答案 0 :(得分:9)

对于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' ) );

然后在您的视图文件中,特定于主页,部分或任何您需要的文件,您只需定义这些块。

例如,在主页视图文件中,您将拥有:

$this->assign( 'head_description', 'this is the description for the home page' );
$this->assign( 'head_keywords', 'this are the keywords for the home page' );

您将对剖面视图或文章视图执行相同的操作。

对于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 );

然后在您的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

答案 1 :(得分:0)

在cakephp 2.x中,在你的布局文件中

<?php 
echo $this->Html->meta('keywords', empty($keywords) ? 'Propcampus.com,Property in Noida, Property in Delhi, Property in Gurgaon,Property in India, Property in Lucknow, Property in Gorakhpur, 1 BHK in Noida, 2 BHK in Noida, 3 BHK in Noida, 4BHK in Noida, ' : $keywords); 
echo $this->Html->meta('description', empty($description) ? 'Propcampus.com is a online realrestate portal' : $description); 
echo $this->Html->meta(array('property' => 'og:title', 'type' => 'meta', 'content' => $this->fetch('title'), 'rel' => null)); 
echo $this->Html->meta(array('property' => 'og:url', 'type' => 'meta', 'content' => empty($url) ? 'Propcampus.com is a online realrestate portal' : $url, 'rel' => null)); 
echo $this->Html->meta(array('property' => 'og:description', 'type' => 'meta', 'content' => empty($description) ? 'Propcampus.com is a online realrestate portal' : $description, 'rel' => null)); 
echo $this->Html->meta(array('property' => 'og:image', 'type' => 'meta', 'content' => empty($imgurl) ? 'Propcampus.com is a online realrestate portal' : $imgurl, 'rel' => null));
echo $this->Html->meta(array('property' => 'og:type', 'type' => 'meta', 'content' => 'website', 'rel' => null));
?>

在您的视图文件中

<?php $this->assign('title', $details['PropertyDetails']['property_type']); ?>
<?php 
      $description = $details['PropertyDetails']['seo_desc'];
      $keywords = $details['PropertyDetails']['seo_keyword'];
      $url = Router::url( $this->here, true );
      $imgurl = $details['PropertyDetails']['property_image'];

    $this->set(compact('keywords','description','url','imgurl')); ?>