短代码内容不可见
我有此代码,该代码基于给定的类“ toc-item”在页面顶部创建一个动态的TOC,用于特定元素。它可以用作过滤器,但我想在短代码中使用它。
我尝试使用add_shortCode('toc_content','create_toc');
,但它没有显示任何内容。知道如何将其更改为短代码函数?多谢!
function create_toc($html) {
$toc = '';
if (is_single()) {
if (!$html) return $html;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
$toc = '<div class="toc-bound">
<div class="toc-ctr">
table of contents
</div>
<ul class="toc">';
$h2_status = 0;
$i = 1;
$xpath = new DOMXpath($dom);
$expression = '//*[contains(concat(" ", normalize-space(@class), " "), " toc-item ")]';
foreach ($xpath->evaluate($expression) as $element) {
if($h2_status){
$toc .= '</li>';
$h2_status = 0;
}
$h2_status = 1;
$toc .= '<li><a href="' . get_the_permalink() . '#toc-' . $i . '">' . $element->textContent . '</a>';
$element->setAttribute('id', 'toc-' . $i);
$i++;
}
if($h2_status){
$toc .= '</li>';
}
$toc .= '</ul></div>';
$html = $dom->saveHTML();
}
return $toc . $html;
}
add_filter('the_content', 'create_toc');
//add_shortcode('toc_content', 'create_toc'); does not work
I have this code that creates a dynamic TOC inside single.php at the top of the page, based on the given class "toc-item" for specific elements. It works as a filter but I would like to use it in a shortcode.
I tried using add_shortcode('toc_content', 'create_toc');
but it does not display anything. Any idea how can I change this into a shortcode function? Thanks a lot!
function create_toc($html) {
$toc = '';
if (is_single()) {
if (!$html) return $html;
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
$toc = '<div class="toc-bound">
<div class="toc-ctr">
table of contents
</div>
<ul class="toc">';
$h2_status = 0;
$i = 1;
$xpath = new DOMXpath($dom);
$expression = '//*[contains(concat(" ", normalize-space(@class), " "), " toc-item ")]';
foreach ($xpath->evaluate($expression) as $element) {
if($h2_status){
$toc .= '</li>';
$h2_status = 0;
}
$h2_status = 1;
$toc .= '<li><a href="' . get_the_permalink() . '#toc-' . $i . '">' . $element->textContent . '</a>';
$element->setAttribute('id', 'toc-' . $i);
$i++;
}
if($h2_status){
$toc .= '</li>';
}
$toc .= '</ul></div>';
$html = $dom->saveHTML();
}
return $toc . $html;
}
add_filter('the_content', 'create_toc');
//add_shortcode('toc_content', 'create_toc'); does not work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
add_shortcode()
回调函数传递了三个参数:$ atts
(快点参数的数组),$ content
快捷代码标签,如果有的话)和$ tag
(短代码的名称)。您的
$ html
参数(如原样)将是一系列短代码参数(我始终为空)。如果您只需要返回DIV元素的短代码,则可能根本不需要任何参数。只需返回元素即可。
The
add_shortcode()
callback function is passed three arguments:$atts
(an array of shortcode arguments),$content
(whatever it is inside the shortcode tags, if anything at all) and$tag
(the name of the shortcode).Your
$html
argument, as it is, would be an array of shortcode arguments (always empty I'm assuming).If you just need the shortcode to return a div element, you probably don't need any argument at all. Just return the element.