变量后的变量锚标签Wordpress

问题描述:

i have a very long article created by a user in Wordpress , i want to show a navigation on every article page with links to the titles of the current article.

e.g: 
<h1 id='title1'>Title 1</h1>
bla bla bla
<h1 id='title2'>Title 2</h1>
bla bla

my navigation on this page would be <a href="#title1">Anchor link to title 1</a>

The example above is how you would hardcode it, but my article text is obviously variable and so are my links, what is the best way to tackle this with php?

Edit: the situation is not exactly like the example, the user puts text into a wordpress text editor field and doesnt want to write html tags, so the navigation needs to be filled with the titles that the user has put in the text field and those link to the variable titles on the page. (with an anchor i assume)

the functionality would be something like Microsoft word: enter image description here

我有一篇由WordPress用户创建的文章很长,我想在每篇文章页面上显示导航 链接到当前文章的标题。 p>

 例如:
&lt; h1 id ='title1'&gt; Title 1&lt; / h1&gt; 
bla bla bla 
&lt; h1 id ='title2'&gt; Title 2&lt;  ; / h1&gt; 
bla bla 
 
我在此页面上的导航将是&lt; a href =“#title1”&gt;锚点链接到标题1&lt; / a&gt; 
  code>  pre> 
 \  n 

上面的例子是你如何硬编码,但我的文章文本显然是可变的,我的链接也是如此,用php解决这个问题的最佳方法是什么? p>

编辑:情况与示例不完全相同,用户将文本放入wordpress文本编辑器字段并且不想编写html标记,因此导航需要填写 用户在文本字段中放置的标题以及链接到页面上的变量标题的标题。 (我假设有一个主持人) strong> p>

这个功能类似Microsoft Word: p> \ n div>

You can filter the content in order to target the different titles, add an ID in a form of a slug using sanitize_title on each of them and build a hierarchical array of those titles in order to display the anchor menu on top of the post.

I just wrote this filter for the example, but it is totally not tested so you may have to debug it a bit and change it depending of your needs. Please note that it works for a 3 level hierarchy maximum.

function add_anchor_menu($content) {
    // First you may want to do some check here to see if this filter should be trigger on the current post...

    $arrayTitles = array();

    // Generate the 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
    );

    // And generate the menu...
    if(count($arrayTitles) > 0) {
        $menu = '<ul id="anchor-menu">';
        foreach($arrayTitles as $level1) {
            $menu .= '<li>';
            $menu .= '<a href="#' . $level1['id'] . '">' . $level1['title'] . '</a>';
            if(count($level1['childs']) > 0) {
                $menu .= '<ul>';
                foreach($level1['childs'] as $level2) {
                    $menu .= '<li>';
                    $menu .= '<a href="#' . $level2['id'] . '">' . $level2['title'] . '</a>';
                    if(count($level2['childs']) > 0) {
                        $menu .= '<ul>';
                        foreach($level2['childs'] as $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');

In your mark up

<h1 id="<?php echo get_the_title(); ?>">Title 1</h1>
<a href="#<?php echo get_the_title(); ?>">Anchor link to title 1</a>

You should esc_url() in the href attribute as well.