Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:22:35+00:00 2026-05-27T23:22:35+00:00

I am trying to build a menu in WordPress that will look like this

  • 0

I am trying to build a menu in WordPress that will look like this HTML below from the Tags/taxonomy on WordPress

Desired Output

<ul id="menu-navigation">
    <li class="no-menu"><a href="">Tag 1</a></li>
    <li class="no-menu"><a href="">Tag 2</a></li>
    <li class="no-menu"><a href="">Tag 3</a></li>
    <li class="no-menu"><a href="">Tag 4</a></li>    
    <li><a href="">More <span class="arrow">▾</span></a>
        <ul>
            <li><a href="">tag 5</a></li>
            <li><a href="">tag 6</a></li>
            <li><a href="">tag 7</a></li>
            <li><a href="">tag 8</a></li>
        </ul>
    </li>
</ul>

Assuming I get a list of 20 tags returned to me as an array()

The goal is the above HTML output, notice..

  • First 4 tags will be wrapped in <li> tags
  • The 5th menu item should be the text More and have <span class="arrow">▾</span> and also the beginning of a new <ul>
  • The 5th through 20th tags will be wrapped in <li> tags but they will be under a sub-menu

I have put this together so far that will almost give me the desired output…

<?php
$posttags = array('tag 1', 'tag 2', 'tag 3', 'tag 4', 'tag 5', 'tag 6', 'tag 7',
    'tag 8', 'tag 9', 'tag 10', 'tag 11', 'tag 12', 'tag 13', 'tag 14', 'tag 15',
    'tag 16', 'tag 17', 'tag 18', 'tag 19', 'tag 20');


$count = 0;
if ($posttags) {
    echo '<ul id="menu-navigation">';
    foreach ($posttags as $tag) {
        $count++;
        // If count less then 5 we show the first 4 tags as top level links
        if ($count < 5) {
            echo '<li class="no-menu"><a href="">' . $tag . '</a></li>';
        }
        // The 5th top level link/menu item will be the More link and begin our Sub-menu
        if ($count == 5) {
            echo '<li><a href="">More <span class="arrow">▾</span></a>';
                echo '<ul>';
        }
                    echo '<li class="no-menu"><a href="">' . $tag . '</a></li>';

    }
                echo '</ul>';
             echo '</li>';
    echo '</ul>';
}

This almost works it give me…

<ul id="menu-navigation">
    <li class="no-menu"><a href="">tag 1</a></li>
    <li class="no-menu"><a href="">tag 1</a></li>
    <li class="no-menu"><a href="">tag 2</a></li>
    <li class="no-menu"><a href="">tag 2</a></li>
    <li class="no-menu"><a href="">tag 3</a></li>
    <li class="no-menu"><a href="">tag 3</a></li>
    <li class="no-menu"><a href="">tag 4</a></li>
    <li class="no-menu"><a href="">tag 4</a></li>
    <li><a href="">More <span class="arrow">?</span></a>
        <ul>
            <li class="no-menu"><a href="">tag 5</a></li>
            <li class="no-menu"><a href="">tag 6</a></li>
            <li class="no-menu"><a href="">tag 7</a></li>
            <li class="no-menu"><a href="">tag 8</a></li>
            <li class="no-menu"><a href="">tag 9</a></li>
            <li class="no-menu"><a href="">tag 10</a></li>
            <li class="no-menu"><a href="">tag 11</a></li>
            <li class="no-menu"><a href="">tag 12</a></li>
            <li class="no-menu"><a href="">tag 13</a></li>
            <li class="no-menu"><a href="">tag 14</a></li>
            <li class="no-menu"><a href="">tag 15</a></li>
            <li class="no-menu"><a href="">tag 16</a></li>
            <li class="no-menu"><a href="">tag 17</a></li>
            <li class="no-menu"><a href="">tag 18</a></li>
            <li class="no-menu"><a href="">tag 19</a></li>
            <li class="no-menu"><a href="">tag 20</a></li>
        </ul>
    </li>
</ul>

So the only problem I am having so far is tag 1-4 are printed twice

Please help me correct this and also if you know a better way to do this I am all ear, appreciate any help, thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:22:36+00:00Added an answer on May 27, 2026 at 11:22 pm

    You were printing the li both when $count<4 and in the end of every iteration, when just printing it once should be sufficient. This should do the trick:

    $count = 0;
    if (is_array($posttags)) {
        echo '<ul id="menu-navigation">';
        foreach ($posttags as $tag) {
            $count++;
            // The 5th top level link/menu item will be the More link and begin our Sub-menu
            if ($count == 5) {
                echo '  <li><a href="">More <span class="arrow">▾</span></a>';
                echo '    <ul>';
            }            // Fix indentation for the items in the sub-menu
            if($count > 4) {
                echo '      ';
            }
            echo '  <li class="no-menu"><a href="">' . $tag . '</a></li>';
    
        }
        if (count($posttags) > 4) {
            echo '    </ul>';
            echo '  </li>';
            echo '</ul>';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a drop-down menu that will show a row to
I'm currently faced with a dilemma. I'm trying to build a menu that will
I'm trying to build a fixed menu like the image. I've searched for some
I am trying to build a simple nested html menu using HAML and am
I'm trying to build template tags that take use a dictionary in settings.py to
I am trying to build a menu using CSS.This is what i have done
Okay, I'm thoroughly stumped on this one. I'm trying to build a menu of
I am trying to build a CSS dropdown menu. The problem is that all
I'm trying to build my own custom dropdown menu that should remember it's vertical
This is the menu I'm trying to build. Now I'm confused and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.