Support » Themes and Templates » HELP! dead-end Get post name or title related to nav_menu_item

  • My apologies if you entered here thinking menu’s custom clases would solve my dilema. Unfortunately they won’t.

    I’d like NOT to depend on adding custom classes to menus because “better not to explain so we can move forward”, and I desperately need to format every icon different, using the page name, NOT the ID.

    THE PROBLEM is nav_menu_items don’t have post-title nor post-name, AT LEAST they are different than the post/page they are associated with (sloppy way of prevent conflicts in DB, faster than filtering functions)

    I found a function that adds my own class-name hooking into the function that constructs the menu_items list, but it processes nav_menu_items, not pages, which DON’T HAVE post-title or post-name, so I have to get the associated post ID somehow (I don’t like pure SQL queries)

    // clears generated classes and adds (tries to) post_name to it
    function page_name_class( $css_class, $post ) {
    	$css_class = array();
       $css_class[] = 'pagename_' . sanitize_title_with_dashes( $post->post_title );
       $css_class[] = 'menuorder-' . $post->menu_order;
       return $css_class;
    }
    add_filter( 'nav_menu_css_class', 'page_name_class', 10, 2 );
    add_filter( 'page_css_class', 'page_name_class', 10, 2 );

    So the question is:

    How do I get the actual post-ID or better the post-title for that function when it loops through nav_menu_items which lack of post-id or page-title/name?

Viewing 7 replies - 1 through 7 (of 7 total)
  • If you are getting the post?

    $css_class[] = 'postid-' . $post->ID;
    $css_class[] = 'slug-' . $post->'post_name';

    OK read it again, you will have to get the link from the menu item, href= then you can get the post based on the slug or id in the url, for each menu item, I think that you will have to have a SQL query.

    If you are using permalink structure say %postname%, you will need to get the url from the menu item.

    Page = http://digitalraindrops.net/atmosphere/
    post = http://digitalraindrops.net/2011/09/twenty-eleven-three-menus/

    Get the last path in the url and call a function to return the title.

    This is a function I have used to return the link from the slug, you can change it to return the title.

    function drfb_post_link( $slug ) {
    	if( !$slug ) return "";
    	global $wpdb;
    	$postid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name ='{$slug}' AND post_type = 'post' ");
    	if ( $postid > 0 ) {
    		return get_permalink( $postid );
    	}
    	return 0;
    }

    HTH

    David

    Really???

    Using a SQL was one of my last shots…

    I prefer to stay with WP functions… but if I HAVE to query the DB…

    Isn’t there a proper place to get the original post ID from?
    … like in taxonomies, as arguments somewhere?

    Anybody?… Bueller?

    The link you provided to get the last path in the url… is plain php…

    Are you sure at this stage (when the class is built) the slug/url has already been rewritten as pretty permalink?

    The url for my menu_items are like http://mydomain.com/?p=92

    That’s the url of the nav item, not the page.
    Permalinks are %postname%, though.

    If I enter that url in my browser my blog responds with a “Page Not Found” error.

    How do I get the actual post-ID or better the post-title for that function when it loops through nav_menu_items which lack of post-id or page-title/name?

    You have not made it clear as to what you are trying to do and where, I assume that you are looping through a nav_menu_items array?

    The WordPress Codex for wp_get_nav_menu_items() shows you how to get the url in the example!

    As to the permalinks you would condition based on:
    ?p=92 is the post with the id 92
    ?page_id=95 is the page with the id 95

    If you are using pretty permalinks %postname% then you get the post slug and use the function above.
    /post-slug/

    I desperately need to format every icon different, using the page name, NOT the ID

    You could also create a menu walker, grab the url $item->url pass to a function to find the post or page value, and then add the icon code in the walker, then pass this to wp_nav_menu()

    HTH

    David

    The WordPress Codex for wp_get_nav_menu_items() shows you how to get the url in the example!

    Excuse if my understanding of the menu items is confusing for me, but as far I understand the above page shows you how to get the url of the MENU ITEM, which is not the same than the post. I don’t know about your layout, but mine doesn’t redirect me to the post if I enter that post id in the URL query. It just says “not found”.

    Not the solution I’m looking for.

    The info of the relationship between posts and menu items is stored in a very accessible place, NO NEED of all these workarounds.

    I found the answer!
    Here is the link to my PageNamed Menu Classes code, to get that missing Title on the Menu Items and Page Listings.

    Hi SocialBlogsite,
    Nice find the wp_postmeta table would not be a place I would look for menu parameters, looking at the table you can also get the type post or page from the _menu_item_object

    The values stored might be of some use for others.

    Here are all stored $key values for Menu ‘PostMeta’ for $menuid
    _menu_item_type = post_type
    _menu_item_menu_item_parent = 0
    _menu_item_object_id = 3538
    _menu_item_object = page
    _menu_item_target = ”
    _menu_item_classes = a:1:{i:0;s:0:””;}
    _menu_item_xfn = ”
    _menu_item_url = ”

    So this shows the $menuid relates to a page with the id of 3538

    $post_type = get_post_meta( $menuid, '_menu_item_object', true);
    $post_id = get_post_meta( $menuid, '_menu_item_object_id', true);

    $post_type = page
    $post_id = 3538

    Glad you managed to sort it, I see you have made it a downloadable plugin in your linked post!

    Regards

    David

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘HELP! dead-end Get post name or title related to nav_menu_item’ is closed to new replies.