2017-02-03 99 views
3

我的文档结构:动态更改元/ title标签,根据页面上的JS

<? 
    include('head.php'); 
    include('header.php'); 
    include('index_content.php'); 
    include('footer.php'); 
    include('form.php'); 
    include('js.php'); 
?> 

当我创建了新的一页,我用这个骨架只改变页面的内容。但对于SEO优化,它也有必要改变标题和元标签。根据页面,是否可以使用JavaScript动态更改此设置。或者是最好不要洗澡,写这样的事:

<? 
    include('head.php'); //beginning of the <head> tag 
?> 
    <title>Some title</title> 
    <meta name="description" content="some description"> 
    <meta name="keywords" content="some keywords"> 
<? 
    include('header.php'); - //end of </head> tag 
    include('index_content.php'); 
    include('footer.php'); 
    include('form.php'); 
    include('js.php'); 
?> 
+0

广东话你'“>'? – Hemal

+0

您是否使用页面内容或静态页面的数据库? –

+0

我用https://prerender.io/在我的案例中 –

回答

0

您可以直接拿到php可变进meta标签,如下图所示。

<? 
    include('head.php'); //beginning of the <head> tag 
?> 
    <title>Some title</title> 
    <meta name="description" content="<?php echo $DESCRIPTION;?>">> 
    <meta name="keywords" content="<?php echo $KEYWORDS;?>">> 
<? 
    include('header.php'); - //end of </head> tag 
    include('index_content.php'); 
    include('footer.php'); 
    include('form.php'); 
    include('js.php'); 
?> 

UPDATE

如果你使用jquery,这可能工作

$("meta[name='description']").attr("content","<?php echo $DESCRIPTION;?>"); 
+0

你觉得bot会等待php变量来解决吗? –

+0

这只是一个脚本代码,在浏览器中呈现时,它将显示简单的文本,很容易抓取。不需要bot去php。 – Hemal

+1

这是完美的页面上的dom拥有解决的价值,但机器人将抓取变量“<?php echo $ DESCRIPTION;?>”作为内容这就是我在工作时经验 –