admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 1There 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
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 badges1 Answer
Reset to default 1There 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
本文标签: categoriesHow to order getcategories() in the same order as the menu
版权声明:本文标题:categories - How to order get_categories() in the same order as the menu? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749125346a2319685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论