【问题标题】:Content from div as meta description来自 div 的内容作为元描述
【发布时间】:2014-12-19 15:19:04
【问题描述】:

我正在尝试从 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 页面提供不同的元描述,而无需编辑数百个文件(就像我已经对标题所做的那样......)但基于该段落中的内容,我可以使用脚本添加到我的 header.php 中。

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

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

【问题讨论】:

  • 元描述在哪里?在&lt;meta&gt; 标头中?

标签: php html include meta


【解决方案1】:

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

<script>

var myText = document.getElementsByTagName('p')[0].innerHTML

document.getElementsByTagName('meta')[0].setAttribute("content", myText);

</script>

【讨论】:

  • 谢谢!这正是我所需要的。
  • 实际上我发现我的工作有点毫无意义,因为搜索引擎不运行 JS。还有很多东西要学。
【解决方案2】:

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

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

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

如果您将服务器上的内容放入“p”标签中,那么将其放入元标签中很容易进行更改

【讨论】:

  • 不,'p' 标签内的内容就在 .php 文件中,如静态 html。
  • 但我假设你在 php 端有你想要的内容?
猜你喜欢
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 2014-05-05
  • 1970-01-01
  • 2011-07-25
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多