短代码内容不可见

发布于 2025-02-01 20:17:20 字数 1550 浏览 4 评论 0原文

我有此代码,该代码基于给定的类“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2025-02-08 20:17:20

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文