- 資訊首頁(yè) > 互聯(lián)網(wǎng) > 主機資訊 >
- 使用純代碼給WordPress添加文章目錄功能,支持快速
wordpress是一款強大的博客系統,支持各種DIY,今天我就給大家帶來(lái)使用純代碼給Wordpress添加文章目錄的功能,使看文章更簡(jiǎn)單!并且支持點(diǎn)擊目錄標題快速定位到文章頁(yè)面指定內容點(diǎn)。
把以下代碼添加到主題的Functions.php文件中
//主機鎮-www.zhujizhen.com //文章目錄 function article_index($content) { $matches = array(); $ul_li = ''; $r = '/<h([2-6]).*?>(.*?)</h[2-6]>/is'; if(is_single() && preg_match_all($r, $content, $matches)) { foreach($matches[1] as $key => $value) { $title = trim(strip_tags($matches[2][$key])); $content = str_replace($matches[0][$key], '<h' . $value . ' id="title-' . $key . '">'.$title.'</h2>', $content); $ul_li .= '<li><a href="#title-'.$key.'" title="'.$title.'">'.$title."</a></li>n"; } $content = "n<div id="article-index"> <strong>文章目錄</strong> <ul id="index-ul">n" . $ul_li . "</ul> </div>n" . $content; } return $content; } add_filter( 'the_content', 'article_index' );
把以下代碼添加到主題的“main.css”文件最底部中
#article-index { -moz-border-radius: 6px 6px 6px 6px; border: 1px solid #DEDFE1; float: right; margin: 0 0 15px 15px; padding: 0 6px; width: 200px; line-height: 23px; } #article-index strong { border-bottom: 1px dashed #DDDDDD; display: block; line-height: 30px; padding: 0 4px; } #index-ul { margin: 0; padding-bottom: 10px; } #index-ul li { background: none repeat scroll 0 0 transparent; list-style-type: disc; padding: 0; margin-left: 20px; }
最終效果可以查看本站文章
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站