Ask your WordPress questions! Pay money and get answers fast! (more info)

Altering the nav output

  • SOLVED

Hello,

Currently I'm using this code to include the post/page/post-type name into the nav li id:

add_filter( 'nav_menu_item_id', 'cor_post_meta_nav_menu', 10, 2 );
function cor_post_meta_nav_menu( $id, $item ) {
$content_page_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
$content_page = get_post( $content_page_id );
$id = $content_page->post_name;
return "nav-$id";
}


Would it perhaps be possible to alter this snippet to do this for categories aswell?

In example, this would be the new output:
<li id="nav-uncategorized"><a href="http://mydomain.tld/category/uncategorized/">Uncategorized</a></li>

Answers (4)

2012-06-26

Hai Bui answers:

Here you go:
add_filter( 'nav_menu_item_id', 'cor_post_meta_nav_menu', 10, 2 );

function cor_post_meta_nav_menu( $id, $item ) {

if ($item->object == 'category') {
$id = strtolower(get_the_category_by_ID($item->object_id));
}
else {
$content_page_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
$content_page = get_post( $content_page_id );
$id = "nav-".$content_page->post_name;
}
return $id;

}


Cor comments:

exactly :)

much appreciated!

2012-06-26

AdamGold answers:

Try:
add_filter( 'nav_menu_item_id', 'cor_post_meta_nav_menu', 10, 2 );

function cor_post_meta_nav_menu( $id, $item ) {

$content_page_id = get_post_meta( $item->ID, '_menu_item_object_id', true );

$post_categories = wp_get_post_categories( $content_page_id );

foreach($post_categories as $c){
$cat = get_category( $c );
return "nav-" . $cat->name;
}

}


This will only return one the first category of the post though. Do you want to get all categories?


Cor comments:

Hi Adam,

Not exactly.

This is an example of what I currently have: http://mydomain.tld/default/

which outputs:

<nav role="navigation">
<ul>
<li id="nav-home" class="active"><a href="http://mydomain.tld/default/">Home</a></li>
<li id="nav-sample-page"><a href="http://mydomain.tld/default/sample-page">Sample Page</a>
<ul class="sub-menu">
<li id="nav-hallo-wereld"><a href="http://mydomain.tld/default/hallo-wereld">Hallo wereld</a></li>
<li id="nav-een-tweede-bericht"><a href="http://mydomain.tld/default/een-tweede-bericht">Een tweede bericht</a></li>
</ul>
</li>
<li id="nav-hallo-wereld"><a href="http://mydomain.tld/default/categorie/ongecategoriseerd">Uncategorized</a></li>
</ul>
</nav>


ideally it should output

<nav role="navigation">
<ul>
<li id="nav-home" class="active"><a href="http://mydomain.tld/default/">Home</a></li>
<li id="nav-sample-page"><a href="http://mydomain.tld/default/sample-page">Sample Page</a>
<ul class="sub-menu">
<li id="nav-hallo-wereld"><a href="http://mydomain.tld/default/hallo-wereld">Hallo wereld</a></li>
<li id="nav-een-tweede-bericht"><a href="http://mydomain.tld/default/een-tweede-bericht">Een tweede bericht</a></li>
</ul>
</li>
<li id="uncategorized"><a href="http://mydomain.tld/default/category/uncategorized">Uncategorized</a></li>
</ul>
</nav>


each post has only one category, so only the first category of the post would be more than fine


AdamGold comments:

Wait so what does my code output?


Cor comments:

<nav role="navigation">
<ul>
<li class="active"><a href="http://mydomain.tld/default/">Home</a></li>
<li><a href="http://mydomain.tld/default/sample-page">Sample Page</a>
<ul class="sub-menu">
<li id="nav-Uncategorized"><a href="http://mydomain.tld/default/hallo-wereld">Hallo wereld</a></li>
<li id="nav-Uncategorized"><a href="http://mydomain.tld/default/een-tweede-bericht">Een tweede bericht</a></li>
</ul>
</li>
<li id="nav-Uncategorized"><a href="http://mydomain.tld/default/category/uncategorized">uncategorized</a></li>
</ul>
</nav>


AdamGold comments:

And what is being echoed without my code?


Cor comments:

with the code from the question it outputs
<nav role="navigation">
<ul>
<li id="nav-home" class="active"><a href="http://mydomain.tld/default/">Home</a></li>
<li id="nav-sample-page"><a href="http://mydomain.tld/default/sample-page">Sample Page</a>
<ul class="sub-menu">
<li id="nav-hallo-wereld"><a href="http://mydomain.tld/default/hallo-wereld">Hallo wereld</a></li>
<li id="nav-een-tweede-bericht"><a href="http://mydomain.tld/default/een-tweede-bericht">Een tweede bericht</a></li>
</ul>
</li>
<li id="nav-hallo-wereld"><a href="http://mydomain.tld/default/category/uncategorized">Uncategorized</a></li>
</ul>
</nav>


without the code from the question it outputs
<nav role="navigation">
<ul>
<li class="active"><a href="http://mydomain.tld/default/">Home</a></li>
<li><a href="http://mydomain.tld/default/sample-page">Sample Page</a>
<ul class="sub-menu">
<li><a href="http://mydomain.tld/default/hallo-wereld">Hallo wereld</a></li>
<li><a href="http://mydomain.tld/default/een-tweede-bericht">Een tweede bericht</a></li>
</ul>
</li>
<li><a href="http://mydomain.tld/default/category/uncategorized">Uncategorized</a></li>
</ul>
</nav>


PS. sorry for the short replies (I'm currently at the daytime job)


AdamGold comments:

Sorry then I don't understand what you're trying to achieve. What's wrong with my code?

2012-06-26

Gabriel Reguly answers:

Hi Cor,

Do you mean altering the output from <?php wp_list_categories( $args ); ?>

Then I suggest a custom walker.

Please let me know what categories you mean.

Regards,
Gabriel


Gabriel Reguly comments:

Hi Cor,

Please try this


add_filter( 'nav_menu_item_id', 'cor_post_meta_nav_menu', 10, 2 );
function cor_post_meta_nav_menu( $id, $item ) {
if ( 'http://mydomain.tld/default/category/' == substr( $item->url, 0 , 36) ) {
$id = substr( $item->url, 37)
} else {
$content_page_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
$content_page = get_post( $content_page_id );
$id = $content_page->post_name;
}
return "nav-$id";
}


Regards,
Gabriel

2012-06-26

Arnav Joy answers:

how do you producing

<li id="nav-uncategorized"><a href="http://mydomain.tld/category/uncategorized/">Uncategorized</a></li>