动态更改元/标题标记,具体取决于具有js的页面

时间:2017-02-03 06:27:23

标签: javascript php html meta

我有文件的结构:

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

1 个答案:

答案 0 :(得分: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');
?>

<强>更新

如果您使用jquery,这可能会起作用

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