为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
撒科打诨 2023-09-15 09:47:43
据说 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;    } 如何在适当的块中插入类别列表?
查看完整描述

1 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

有两种使用方法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);

查看完整回答
反对 回复 2023-09-15
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信