douweida2669 2015-09-30 10:37
浏览 17
已采纳

变量后的变量锚标签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

  • 写回答

2条回答 默认 最新

  • dougui1977 2015-09-30 14:55
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)