我想问一些我不知道是否有可能的事情
我将首先以html的形式向您展示我需要的内容。
<nav class="nav-categories">
<ul>
<li class="no-filter">
<a href="#"> <strong>Category Name</strong> <span class="count">25</span></a>
</li>
<li class="no-filter">
<a href="#"> <strong>Category Name</strong> <span class="count">25</span></a>
</li>
<li class="no-filter">
<a href="#"> <strong>Category Name</strong> <span class="count">25</span></a>
</li>
<li class="no-filter">
<a href="#"> <strong>Category Name</strong> <span class="count">25</span></a>
</li>
</ul>
<div class="hidden-content" id="hidden-categories">
<ul>
<li class="no-filter">
<a href="/category/38/massaggi" title="Massaggi">Category Name<span class="count">25</span></a>
</li>
<li class="no-filter">
<a href="/category/38/massaggi" title="Massaggi">Category Name<span class="count">25</span></a>
</li>
<li class="no-filter">
<a href="/category/38/massaggi" title="Massaggi">Category Name<span class="count">25</span></a>
</li>
<li class="no-filter">
<a href="/category/38/massaggi" title="Massaggi">Category Name<span class="count">25</span></a>
</li>
</ul>
</div>
<button class="category-toggle" data-action="content-toggle" data-target="#hidden-categories" data-more="Display More Categories" data-less="Display Less Categories">Display All Categories</button>
</nav>
正如您在上面的代码中看到的那样,有2个元素有两个菜单,但其中一个是可见的,一个是隐藏的,我可以通过点击按钮显示隐藏内容。
使用下面的代码我可以显示所有wordpress类别,但很高兴知道是否可以在第一个div中显示一半类别而在隐藏内容类中显示另一半。
<?php
$categories = get_categories();
echo '<nav class="nav-categories"><ul>';
foreach($categories as $category) {
echo '<li class="no-filter"><a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'';
echo '<span class="count">' . $category->count . '</span>';
echo '</a></li>';
}
echo '</ul></nav>';
?>
三江源
答案 0 :(得分:0)
是的,这是可能的,并且有很多方法可以实现这一点,可能是最简单和最快的(这是非常天真的,我只是添加了一些添加和修改你的代码,概念只是找到索引是数组长度的一半),假设您的原始代码有效,这应该有效,如果它没有,那么我确定您可以理解这个概念。
<?php
$categories = get_categories();
$firstNav = "";
$secondNav = "<div class="hidden-content" id="hidden-categories">";
echo '<nav class="nav-categories"><ul>';
$maxIndex = count($categories) - 1;
$half = floor($maxIndex / 2); //To get the middle of the array, or you can use ceil();
$curIndex = 0;
foreach($categories as $category) {
if ($curIndex <= $half) {
$firstHalf .= '<li class="no-filter"><a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'';
$firstHalf .= '<span class="count">' . $category->count . '</span>';
$firstHalf .= '</a></li>';
}
else {
$secondHalf .= '<li class="no-filter"><a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'';
$secondHalf .= '<span class="count">' . $category->count . '</span>';
$secondHalf .= '</a></li>';
}
$curIndex++;
}
$firstHalf .= "</ul>";
$secondHalf .= "</div>";
echo $firstHalf;
echo $secondHalf;
echo '</ul></nav>';
?>