php - Variable Anchor tags inside a variable post Wordpress -


i have long article created user in wordpress , want show navigation on every article page links titles of current article.

e.g:  <h1 id='title1'>title 1</h1> bla bla bla <h1 id='title2'>title 2</h1> bla bla  navigation on page <a href="#title1">anchor link title 1</a> 

the example above how hardcode it, article text variable , links, best way tackle php?

edit: situation not example, user puts text wordpress text editor field , doesnt want write html tags, navigation needs filled titles user has put in text field , link variable titles on page. (with anchor assume)

the functionality microsoft word: enter image description here

you can filter content in order target different titles, add id in form of slug using sanitize_title on each of them , build hierarchical array of titles in order display anchor menu on top of post.

i wrote filter example, totally not tested may have debug bit , change depending of needs. please note works 3 level hierarchy maximum.

function add_anchor_menu($content) {     // first may want check here see if filter should trigger on current post...      $arraytitles = array();      // generate ids...     $content = preg_replace_callback(         '#<h([1-3])>(.*?)<\/h[1-3]>#',         function($matches) {             $id = sanitize_title($matches[2]);             $meta = array('id' => $id, 'title' => $matches[2], 'childs' => array());             if((int)$matches[1] == 1) {                 array_push($arraytitles, $meta);             } elseif((int)$matches[1] == 2) {                 end($arraytitles);                 array_push($arraytitles[key($arraytitles)]['childs'], $meta);             } else {                 end($arraytitles);                 end($arraytitles[key($arraytitles)]['childs']);                 array_push($arraytitles[key($arraytitles)]['childs'][key($arraytitles[key($arraytitles)])], $meta);             }             return '<h' . $matches[1] . ' id="' . $id . '">' . $matches[2] . '</h' . $matches[1] . '>';         },         $content     );      // , generate menu...     if(count($arraytitles) > 0) {         $menu = '<ul id="anchor-menu">';         foreach($arraytitles $level1) {             $menu .= '<li>';             $menu .= '<a href="#' . $level1['id'] . '">' . $level1['title'] . '</a>';             if(count($level1['childs']) > 0) {                 $menu .= '<ul>';                 foreach($level1['childs'] $level2) {                     $menu .= '<li>';                     $menu .= '<a href="#' . $level2['id'] . '">' . $level2['title'] . '</a>';                     if(count($level2['childs']) > 0) {                         $menu .= '<ul>';                         foreach($level2['childs'] $level3) {                             $menu .= '<li><a href="#' . $level3['id'] . '">' . $level3['title'] . '</a></li>';                         }                         $menu .= '</ul>';                     }                     $menu .= '</li>';                 }                 $menu .= '</ul>';             }             $menu .= '</li>';         }         $menu .= '<ul>';         $content = $menu . $content;     }      return $content; } add_filter('the_content', 'add_anchor_menu'); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -