在文章标题中添加ID标签(Wordpress)。

问题描述 投票:0回答:1

我一直在努力寻找一种方法,通过在子主题的functions.php文件中放置一个函数来为文章标题添加一个ID标签。

我试过下面的代码,它能很好地将任何没有ID的h1到h6(& 留下任何有ID的)的ID添加到帖子内容中--但我找不到一个方法将ID添加到实际显示的帖子标题中。

function auto_id_headings( $content ) {

    $content = preg_replace_callback( '/(\<h[1-6](.*?))\>(.*)(<\/h[1-6]>)/i', function( $matches ) {
        if ( ! stripos( $matches[0], 'id=' ) ) :
            $matches[0] = $matches[1] . $matches[2] . ' id="' . sanitize_title( $matches[3] ) . '">' . $matches[3] . $matches[4];
        endif;
        return $matches[0];
    }, $content );

    return $content;

}
add_filter( 'the_content', 'auto_id_headings' );

我以为这只是简单的改变 the_content 中的 "add_filter "部分改为 the_title 但这并不奏效! 顺便说一下,我想做这个的页面在这里。https:/www.futureproofpromotions.comreaction-form

有谁能帮忙吗?

我已经附上了页面的chrome开发者工具的截图,下面解释一下我想实现的目标。

enter image description here

php wordpress function id
1个回答
0
投票

钩子the_content返回所有的文章内容(包括HTML),但钩子the_title返回你的值从post_title字段(wp_post表).所以,如果你想添加标签id到id或类DOM元素,你需要在你的孩子的主题:1.复制single.php(或其他页面模板,你需要做出改变)在你的孩子的主题2.找到的地方与the_title()函数3.改变H1类(添加你的函数

© www.soinside.com 2019 - 2024. All rights reserved.