categories - How to order get_categories() in the same order as the menu?

admin2025-06-05  7

I'm using a theme and I noticed that it dynamically displays an array of the categories in the menu:

I need the same list of categories but within different HTML tags for my footer. Currently this is what I have in my footer:

footer.php:

<?php
      $categories = get_categories();
      foreach($categories as $category) {
        echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
      }
?>

As you can see, it's not in the same order as the one in the menu.

I tried to use that as a hint to how I would loop through the categories. However, when I try to find the template for the header, I notice that it just points to this and I don't see any of the PHP / HTML:

    <nav class="main-navigation">

      <?php
        wp_nav_menu( array(
          'theme_location' => 'header-menu',
          'fallback_cb' => 'false',
        ) );
      ?>

    </nav><!-- /.main-navigation -->

I tried looking everywhere for a header-menu.php file but it doesn't seem to exist, possibly because it's coming from the theme..?

register_nav_menus( array(
  'header-menu' => esc_html__( 'Header', 'neori' ),
) );

Here's a pic of what I want:

I'm using a theme and I noticed that it dynamically displays an array of the categories in the menu:

I need the same list of categories but within different HTML tags for my footer. Currently this is what I have in my footer:

footer.php:

<?php
      $categories = get_categories();
      foreach($categories as $category) {
        echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
      }
?>

As you can see, it's not in the same order as the one in the menu.

I tried to use that as a hint to how I would loop through the categories. However, when I try to find the template for the header, I notice that it just points to this and I don't see any of the PHP / HTML:

    <nav class="main-navigation">

      <?php
        wp_nav_menu( array(
          'theme_location' => 'header-menu',
          'fallback_cb' => 'false',
        ) );
      ?>

    </nav><!-- /.main-navigation -->

I tried looking everywhere for a header-menu.php file but it doesn't seem to exist, possibly because it's coming from the theme..?

register_nav_menus( array(
  'header-menu' => esc_html__( 'Header', 'neori' ),
) );

Here's a pic of what I want:

Share Improve this question edited Dec 5, 2018 at 20:36 bigpotato asked Dec 5, 2018 at 19:14 bigpotatobigpotato 3352 gold badges6 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There is no header-menu.php, that bit of code is creating a WP Menu, have a look at this.

You could create another menu for the footer. Or if you need more control of the markup, you could use your category code, which uses get_categories() and then sort them how you like. More info on get_categories().

Here is an example of sorting by name.

$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );

So your the entire code block would look like this.

$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC'
) );

foreach($categories as $category) {
  echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
}

Edit (Copy Main Navigation in Footer): You can copy your main navigation to the footer by adding this code in your footer.php.

<nav class="footer-navigation">
    <?php
        wp_nav_menu( array(
        'theme_location' => 'header-menu',
        'fallback_cb' => 'false',
        ) );
    ?>
</nav>

Edit 2 (Array of Nav Items):

You can build your own array using wp_get_nav_menu_items(). Docs here
and Examples here

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749125373a316579.html

最新回复(0)