[英]Adding an ID tag to a post title (Wordpress)
我一直在嘗試通過在子主題的 functions.php 文件中放置 function 來找到一種將 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' );
我認為這就像將代碼的“add_filter”部分中的the_title
更改為the_content
一樣簡單,但這並沒有奏效:僅供參考,我正在嘗試執行此操作的頁面在這里: https://www.futureproofpromotions。 com/reaction-form/
有人可以幫忙嗎?
我已經包含了該頁面的 chrome 開發人員工具的屏幕截圖,解釋了我在下面嘗試實現的目標:
掛鈎 the_content 返回所有帖子內容(包括 HTML),但掛鈎 the_title 從 post_title 字段(wp_post 表)返回您的值。 因此,如果您想將標簽 id 添加到 id 或 class DOM 元素,您需要在您的子主題中進行更改: 1. 在您的子主題中復制 single.php(或您需要進行更改的其他頁面模板) 2. 找到具有the_title() function 3. 更改 h1 class (添加您的功能)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.