【发布时间】:2016-01-05 16:45:07
【问题描述】:
我正在做一个项目,我需要列出所有类别、子类别(儿童、孙子等到大约 5 级)和其中的帖子。目前我已经设法让该网站正常运行,但问题是该网站需要几分钟才能加载。
为了明确我希望网站的外观,这里有一张图:
类别 子类别 子类别 内容 子类别 内容 子类别 你得到了图片
这是代码:
<?php
function listing($parentcat, $list_id='', $list_name='', $path=false) {
// parentcat is the desired category parent category, which is defined because the function is called a few times with different sets on the page.
// list_id, list_name and path are used for purposes not related to the question
echo "<ul><li name='$list_name'><h2><a href='#$list_name'>$list_name</a></h2>";
}
$args = array(
'parent' => $parentcat,
'include' => $cat_ids,
'hide_empty' => 1,
'orderby' => 'id'
);
$categories = get_categories( $args );
echo "<ul>";
foreach ($categories as $cat) {
if ($cat->cat_name != 'Uncategorized') {
$flat_path = substr(get_category_parents($cat->cat_ID, false, ' »' ), 14);
$catnam = $cat->cat_name;
$listtitle = ($path ? $flat_path : $catnam);
echo ('<li name="' . $cat->cat_ID . '"><h2><a href="#' . $cat->cat_ID . '">' . $listtitle . '</a></h2>' );
if (get_posts( "category_name=$catnam" ) ) {
if (have_posts()) : while (have_posts()) : the_post();
if (in_category($cat->cat_ID)) {
echo '<ul><li><div>';
the_content();
echo '</div></li></ul>';
} endwhile;
else :
_e('Empty list');
endif;
}
// Here's a recursive call for the function
listing($cat->cat_ID);
echo '</li>';
}
}
echo '</ul>';
}
?>
【问题讨论】:
-
已经尝试使用模板部分而不是函数,为此递归创建一个模板和一个子模板。看到这个stackoverflow.com/questions/32585441/…有类似的东西。但我在他的网站。