首页 文章

来自div的内容作为元描述

提问于
浏览
0

我正在尝试从p元素中获取内容作为元描述 . 我试着调整我在这里找到但没有运气的东西 . Show content from DIV in a META-tag我所拥有的是一堆包含页眉和页脚的.php文件 .

<?php
 $title="Page title";
 include_once("/includes/header.php");
?>
<div class="container">
<p>This content i want in meta description and it's different in each php file.</p>
</div>
<?php
 include_once("/includes/bottom.php");
?>

我想要实现的是为每个.php页面设置不同的元描述,我没有编辑100个文件(正如我已经使用 Headers ...)但是基于该段落内的内容使用脚本我可以添加到我的header.php文件 .

我试图尽可能清楚地做到这一点 . 提前感谢您的帮助 . 编辑我想要的描述在header.php中

<meta name="description" content="This is where i want that description" />

2 回答

  • 1

    你甚至不需要PHP . 使用Javascript,您可以检索段落标记的值,然后将元标记更改为具有此值 . 像这样的东西:

    <script>
    
    var myText = document.getElementsByTagName('p')[0].innerHTML
    
    document.getElementsByTagName('meta')[0].setAttribute("content", myText);
    
    </script>
    
  • 1

    你可以直接将内容注入标签,就像这样

    <meta name="description" content="<?php echo someContent; ?>" />
    

    (显然你需要编辑元标记的类型等)

    如果你有服务器上的内容放入'p'标签,那么将它放在元标签中是一个很容易的改变

相关问题