$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - How to order get_categories() in the same order as the menu?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far