0

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

enter image description here

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

enter image description here

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:

enter image description here

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

|improve this answer|||||
  • Hi Riddle. Would there be a way to sort the categories in the same order as the menu though? From the looks of the header menu (the 1st pic I pasted), it doesn't look like it's in alphabetical order. Is there another field I'm not aware of like sortOrder or something? – bigpotato Dec 5 '18 at 20:28
  • Oh! I just realized I need to grab the menu items, not the categories. Is there a way to do this? – bigpotato Dec 5 '18 at 20:32
  • What are you trying todo? Can you just duplicate the same menu in the footer? – RiddleMeThis Dec 5 '18 at 20:34
  • I just want an array of the menu items in the same order, loop through each, and output something like <a href="$menu_item->url">$menu_item.title</a> – bigpotato Dec 5 '18 at 20:35
  • Updated to include a pic (except it wouldn't be Culture, Web, etc, it would be Editorial, World, etc like the header menu) – bigpotato Dec 5 '18 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.