Changing default html structure in WordPress nav menu
By default, as everybody knows, Wordpress create a nav structure when you use the default menu function.
I have 10 items "li" (one per category) in the primary menu, and i'm trying add a different image into each "li". But I wish:
- Maintain the links to the categories into the ahref
- The content should be included into the ahref
Summarizing:
I want transform this
<li class="casilleroCat">
<a href="https://categoryurl.com">Name of category2</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">Name of category2</a>
</li>
into this
<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">
<img src="https://url.com/customimage2.jpg" alt="name of category2">
<br>
<span>Name of category2</span>
</a>
</li>
For that, I tried adding the following code in functions.php
/** FIRST, I CREATED THE MENUS **/
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
And now, i'm lost because i don't know what it's the best way to achieve this. Because if I add a code like this i'll lost the links created automatically by WordPress and I have to create a different function for each image.
function add_customHTML($items, $args) {
if ($args->theme_location == 'Header Menu') {
// My html
$item = '<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>';
$items = $item.$items;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_customHTML', 10, 2 );
wordpress
add a comment |
By default, as everybody knows, Wordpress create a nav structure when you use the default menu function.
I have 10 items "li" (one per category) in the primary menu, and i'm trying add a different image into each "li". But I wish:
- Maintain the links to the categories into the ahref
- The content should be included into the ahref
Summarizing:
I want transform this
<li class="casilleroCat">
<a href="https://categoryurl.com">Name of category2</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">Name of category2</a>
</li>
into this
<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">
<img src="https://url.com/customimage2.jpg" alt="name of category2">
<br>
<span>Name of category2</span>
</a>
</li>
For that, I tried adding the following code in functions.php
/** FIRST, I CREATED THE MENUS **/
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
And now, i'm lost because i don't know what it's the best way to achieve this. Because if I add a code like this i'll lost the links created automatically by WordPress and I have to create a different function for each image.
function add_customHTML($items, $args) {
if ($args->theme_location == 'Header Menu') {
// My html
$item = '<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>';
$items = $item.$items;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_customHTML', 10, 2 );
wordpress
this could be useful: digwp.com/2011/11/html-formatting-custom-menus
– DaFois
Nov 25 '18 at 18:03
@DaFois Thank you, I added more info in my edited post. Issue resolved, but I lost page ancestor and related li info
– Carlos Hervas Ortega
Nov 26 '18 at 5:12
I think that the main problem is solved. I'm going to open another post to the secondary problem. Thank you
– Carlos Hervas Ortega
Nov 26 '18 at 10:34
add a comment |
By default, as everybody knows, Wordpress create a nav structure when you use the default menu function.
I have 10 items "li" (one per category) in the primary menu, and i'm trying add a different image into each "li". But I wish:
- Maintain the links to the categories into the ahref
- The content should be included into the ahref
Summarizing:
I want transform this
<li class="casilleroCat">
<a href="https://categoryurl.com">Name of category2</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">Name of category2</a>
</li>
into this
<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">
<img src="https://url.com/customimage2.jpg" alt="name of category2">
<br>
<span>Name of category2</span>
</a>
</li>
For that, I tried adding the following code in functions.php
/** FIRST, I CREATED THE MENUS **/
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
And now, i'm lost because i don't know what it's the best way to achieve this. Because if I add a code like this i'll lost the links created automatically by WordPress and I have to create a different function for each image.
function add_customHTML($items, $args) {
if ($args->theme_location == 'Header Menu') {
// My html
$item = '<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>';
$items = $item.$items;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_customHTML', 10, 2 );
wordpress
By default, as everybody knows, Wordpress create a nav structure when you use the default menu function.
I have 10 items "li" (one per category) in the primary menu, and i'm trying add a different image into each "li". But I wish:
- Maintain the links to the categories into the ahref
- The content should be included into the ahref
Summarizing:
I want transform this
<li class="casilleroCat">
<a href="https://categoryurl.com">Name of category2</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">Name of category2</a>
</li>
into this
<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>
<li class="casilleroCat">
<a href="https://categoryurl2.com">
<img src="https://url.com/customimage2.jpg" alt="name of category2">
<br>
<span>Name of category2</span>
</a>
</li>
For that, I tried adding the following code in functions.php
/** FIRST, I CREATED THE MENUS **/
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
And now, i'm lost because i don't know what it's the best way to achieve this. Because if I add a code like this i'll lost the links created automatically by WordPress and I have to create a different function for each image.
function add_customHTML($items, $args) {
if ($args->theme_location == 'Header Menu') {
// My html
$item = '<li class="casilleroCat">
<a href="https://categoryurl.com">
<img src="https://url.com/customimage1.jpg" alt="name of cateogry">
<br>
<span>Name of category</span>
</a>
</li>';
$items = $item.$items;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'add_customHTML', 10, 2 );
wordpress
wordpress
edited Nov 26 '18 at 10:37
Carlos Hervas Ortega
asked Nov 25 '18 at 17:45
Carlos Hervas OrtegaCarlos Hervas Ortega
488
488
this could be useful: digwp.com/2011/11/html-formatting-custom-menus
– DaFois
Nov 25 '18 at 18:03
@DaFois Thank you, I added more info in my edited post. Issue resolved, but I lost page ancestor and related li info
– Carlos Hervas Ortega
Nov 26 '18 at 5:12
I think that the main problem is solved. I'm going to open another post to the secondary problem. Thank you
– Carlos Hervas Ortega
Nov 26 '18 at 10:34
add a comment |
this could be useful: digwp.com/2011/11/html-formatting-custom-menus
– DaFois
Nov 25 '18 at 18:03
@DaFois Thank you, I added more info in my edited post. Issue resolved, but I lost page ancestor and related li info
– Carlos Hervas Ortega
Nov 26 '18 at 5:12
I think that the main problem is solved. I'm going to open another post to the secondary problem. Thank you
– Carlos Hervas Ortega
Nov 26 '18 at 10:34
this could be useful: digwp.com/2011/11/html-formatting-custom-menus
– DaFois
Nov 25 '18 at 18:03
this could be useful: digwp.com/2011/11/html-formatting-custom-menus
– DaFois
Nov 25 '18 at 18:03
@DaFois Thank you, I added more info in my edited post. Issue resolved, but I lost page ancestor and related li info
– Carlos Hervas Ortega
Nov 26 '18 at 5:12
@DaFois Thank you, I added more info in my edited post. Issue resolved, but I lost page ancestor and related li info
– Carlos Hervas Ortega
Nov 26 '18 at 5:12
I think that the main problem is solved. I'm going to open another post to the secondary problem. Thank you
– Carlos Hervas Ortega
Nov 26 '18 at 10:34
I think that the main problem is solved. I'm going to open another post to the secondary problem. Thank you
– Carlos Hervas Ortega
Nov 26 '18 at 10:34
add a comment |
1 Answer
1
active
oldest
votes
Finally, I achieved with this snippet
function clean_custom_menus() {
$menu_name = 'header-menu'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<nav>' ."n";
$menu_list .= "tttt". '<ul>' ."n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "ttttt". '<li class="casilleroCat"><a href="'. $url .'"><img src="imagen'. $a++ .'.jpg"><br><span>'. $title .'</span></a></li>' ."n";
}
$menu_list .= "tttt". '</ul>' ."n";
$menu_list .= "ttt". '</nav>' ."n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
And calling the function in the page with this code
<?php wp_nav_menu( array( 'theme_location' => 'extra-menu' ) ); ?>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470192%2fchanging-default-html-structure-in-wordpress-nav-menu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Finally, I achieved with this snippet
function clean_custom_menus() {
$menu_name = 'header-menu'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<nav>' ."n";
$menu_list .= "tttt". '<ul>' ."n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "ttttt". '<li class="casilleroCat"><a href="'. $url .'"><img src="imagen'. $a++ .'.jpg"><br><span>'. $title .'</span></a></li>' ."n";
}
$menu_list .= "tttt". '</ul>' ."n";
$menu_list .= "ttt". '</nav>' ."n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
And calling the function in the page with this code
<?php wp_nav_menu( array( 'theme_location' => 'extra-menu' ) ); ?>
add a comment |
Finally, I achieved with this snippet
function clean_custom_menus() {
$menu_name = 'header-menu'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<nav>' ."n";
$menu_list .= "tttt". '<ul>' ."n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "ttttt". '<li class="casilleroCat"><a href="'. $url .'"><img src="imagen'. $a++ .'.jpg"><br><span>'. $title .'</span></a></li>' ."n";
}
$menu_list .= "tttt". '</ul>' ."n";
$menu_list .= "ttt". '</nav>' ."n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
And calling the function in the page with this code
<?php wp_nav_menu( array( 'theme_location' => 'extra-menu' ) ); ?>
add a comment |
Finally, I achieved with this snippet
function clean_custom_menus() {
$menu_name = 'header-menu'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<nav>' ."n";
$menu_list .= "tttt". '<ul>' ."n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "ttttt". '<li class="casilleroCat"><a href="'. $url .'"><img src="imagen'. $a++ .'.jpg"><br><span>'. $title .'</span></a></li>' ."n";
}
$menu_list .= "tttt". '</ul>' ."n";
$menu_list .= "ttt". '</nav>' ."n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
And calling the function in the page with this code
<?php wp_nav_menu( array( 'theme_location' => 'extra-menu' ) ); ?>
Finally, I achieved with this snippet
function clean_custom_menus() {
$menu_name = 'header-menu'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<nav>' ."n";
$menu_list .= "tttt". '<ul>' ."n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "ttttt". '<li class="casilleroCat"><a href="'. $url .'"><img src="imagen'. $a++ .'.jpg"><br><span>'. $title .'</span></a></li>' ."n";
}
$menu_list .= "tttt". '</ul>' ."n";
$menu_list .= "ttt". '</nav>' ."n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
And calling the function in the page with this code
<?php wp_nav_menu( array( 'theme_location' => 'extra-menu' ) ); ?>
answered Nov 26 '18 at 10:36
Carlos Hervas OrtegaCarlos Hervas Ortega
488
488
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470192%2fchanging-default-html-structure-in-wordpress-nav-menu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
this could be useful: digwp.com/2011/11/html-formatting-custom-menus
– DaFois
Nov 25 '18 at 18:03
@DaFois Thank you, I added more info in my edited post. Issue resolved, but I lost page ancestor and related li info
– Carlos Hervas Ortega
Nov 26 '18 at 5:12
I think that the main problem is solved. I'm going to open another post to the secondary problem. Thank you
– Carlos Hervas Ortega
Nov 26 '18 at 10:34