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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$20
Altering the nav output

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>

This question has been answered.

Cor van Noorloos | 06/26/12 at 9:02am Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/26/12
    9:11am
    AdamGold says:

    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?

    Previous versions of this answer: 06/26/12 at 9:11am | 06/26/12 at 9:11am | 06/26/12 at 9:11am

    • 06/26/12 9:29am

      Cor van Noorloos says:

      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

    • 06/26/12 9:31am

      AdamGold says:

      Wait so what does my code output?

    • 06/26/12 9:37am

      Cor van Noorloos says:

      <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>

    • 06/26/12 9:39am

      AdamGold says:

      And what is being echoed without my code?

    • 06/26/12 9:52am

      Cor van Noorloos says:

      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)

    • 06/26/12 10:08am

      AdamGold says:

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

  • avatar
    Last edited:
    06/26/12
    9:12am
    Gabriel Reguly says:

    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

    • 06/26/12 10:38am

      Gabriel Reguly says:

      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

  • avatar
    Last edited:
    06/26/12
    9:16am
    Arnav Joy says:

    how do you producing

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

  • avatar
    Last edited:
    06/26/12
    10:18am
    Hai Bui says:

    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;

    }

This question has expired.



Gabriel Reguly, Cor van Noorloos, Francisco Javier Carazo Gil, Arnav Joy voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.