首页 文章

为所有Wordpress页面的标记添加属性

提问于
浏览
0

如何使用PHP为我的所有Wordpress网站页面的H1标签添加属性?

期望的结果:

<h1 itemprop="name">Title of Article</h1>

2 回答

  • 0

    我想你的意思是帖子中的第一个 h1 ,它动态显示后端定义的帖子或页面的名称?

    这应该可以在页面或帖子模板中使用此代码(可能是 index.phppage.phppost.phpsingle.php 等),默认情况下显示where Headers 的位置(必须在Wordpress loop 内):

    <h1 itemprop="name"><?php the_title(); ?></h1>
    
  • 0

    以下是代码

    $html = new DOMDocument();
        $html->loadHtml('filename');
        $nodes = $html->getElementsByTagName('h1');
        foreach ($nodes as $node) {
                $node->setAttribute('itemprop','name');
        }
        $txt = $html->saveHTML();
    

    如果有效,请告诉我!

    EDIT:

    <?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文件中粘贴上面的代码,然后在浏览器中运行即可查看结果

相关问题