自定义限制类别列表 wordpress


Custom limit categories list wordpress

最近我正在研究wordpress,我的设计有点复杂,在wordpress中找不到功能,特别是打印类别如下:

<ul>
    <li><a href="">Category 1</a></li>
    <li><a href="">Category 2</a></li>
    <li><a href="">Category 3</a></li>
    <li><a href="">More Categories</a>
        <ul>
            <li><a href="">Category 4</a></li>
            <li><a href="">Category 5</a></li>
            <li><a href="">Category 6</a></li>
            <li><a href="">Category 7</a></li>
        </ul>
    </li>
</ul>

此代码应该适用于wordpress帖子的列表。

foreach((get_the_category()) as $cat) {
    echo '<li><a href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a><li>';
}

http://codex.wordpress.org/Function_Reference/get_the_category

你可以

这样做:

$hasmore=false;
$i=0;
foreach((get_the_category()) as $cat) {
  if($i==3)
    {
    $hasmore=true;
    echo '<li><a href="UnansweredCommentQuestion.php">More Categories</a><ul>';
    }
  echo '<li><a href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a></li>';
  $i++;
}
if($hasmore) echo "</ul></li>";

您可以使用以下函数

wp_list_categories()