为什么wp_list_categories()会自动插入到页面顶部?

据说 wp_list_categories() 函数返回一个字符串。但是,它不会返回任何内容,而是在另一个位置激活类别列表的输出。


页面内容生成如下


function build_index_posts(...){

  $html = '';

  $html.= '<div class="one">';

  $html.= '<h1>'.$header_arr['title'].'</h1>';

  $html.= '</div>';


  return $html;

我试图输出一个包含子类别列表的块,但它没有插入到指定位置,而是插入到页面的最开头。


   function build_index_posts(...){

      $html = '';

      $html.= '<div class="one">';

      $html.= '<h1>'.$header_arr['title'].'</h1>';

      $html.= '</div>';

    

      $category = get_queried_object();

      $category_id = $category->term_id;

                    

        $li_args = array(

         'child_of' => $category_id,

         'depth' => 1, 

         'style' => 'none',  

         'hide_empty' => 0,  

         'orderby'      => 'name',  

         'show_count'   => 0,  

         'pad_counts'   => 0, 

         'hierarchical' => 1,  

         'title_li'     => ''  

        );


        $cat_list = wp_list_categories($li_args);

        $cat_list = str_replace('<br>', '', $cat_list);

        

        $html.= '<div style="display: none">';

        $html.= $cat_list;  // NULL ?? 

        $html.= '</div>';


      return $html;

    } 

如何在适当的块中插入类别列表?


撒科打诨
浏览 107回答 1
1回答

万千封印

有两种使用方法wp_list_categories()- 您使用它的方式将立即输出代码中调用它的列表 - 这就是您在页面顶部看到它的原因。如果您想获取类别列表作为变量,则需要传递包含echo值为false(默认为 true)的参数:$li_args = array(     'echo' => false,      /* the rest of your args */);// now you can save the list in a variable.$cat_list = wp_list_categories($li_args);
打开App,查看更多内容
随时随地看视频慕课网APP