如何使用PHP爲我所有的WordPress網站頁面的H1標籤添加屬性?將屬性添加到所有Wordpress頁面的標籤
期望的結果:
<h1 itemprop="name">Title of Article</h1>
如何使用PHP爲我所有的WordPress網站頁面的H1標籤添加屬性?將屬性添加到所有Wordpress頁面的標籤
期望的結果:
<h1 itemprop="name">Title of Article</h1>
下面是代碼
$html = new DOMDocument();
$html->loadHtml('filename');
$nodes = $html->getElementsByTagName('h1');
foreach ($nodes as $node) {
$node->setAttribute('itemprop','name');
}
$txt = $html->saveHTML();
讓我知道,如果它的工作原理!
編輯:
<?php
$html = '<h1>Title of Article</h1>';
$dom = new DOMDocument();
$dom->loadHtml($html);
$nodes = $dom->getElementsByTagName('h1');
foreach ($nodes as $node) {
$node->setAttribute('itemprop','name');
}
$html = $dom->saveHTML();
echo $html;
使用此代碼,你可以看到的結果。只需將上面的代碼粘貼到空白的php文件中並在瀏覽器中運行即可看到結果
你可以用JS做到,如果它符合你的要求我可以建議你用JS的方式。 –
我用jQuery很容易做到這一點。但它沒有出現在谷歌結構化數據測試工具上。所以我真的沒有看到使用JS的重點。 – Kannan