Latest posts in menu
-
Hello!
I would really need some help in resolving a litle problem or maybe is better that I call it an idea. I’d like to add my posts automaticaly to menu, named something like Latest news, only choosing a category for a post. For example, I publish a post about a trip to a lake and I choose a category Latest news. I want autoaticaly post to be published in menu Latest news. By that I mean that I don’t have to manualy put the post in menu.
How can I do this?
-
I’m not sure how it should look like, but you might be better of not using the WordPress menu’s but simply have the menu’s generated based on categories and posts. Simply use WP_Query for getting the posts and get_categories for the categories.
Otherwise you should create a plugin and hook it to save_post or wp_insert_post:
add_action('save_post', 'mycustom_add_post_to_menu'); function mycustom_add_post_to_menu($post_id) { if ( wp_is_post_revision( $post_id ) ) return; wp_update_nav_menu_item($menu_id, 0, $item); }Above you find a simplified version. The $menu_id variable should have the ID of the menu you want to add the item to. The second parameter ‘0’ means you want to add a new item. $item is an array with all details. You can add the following items to the array:
$defaults = array( 'menu-item-parent-id' => 0, 'menu-item-position' => 0, 'menu-item-type' => 'custom', 'menu-item-title' => '', 'menu-item-url' => '', 'menu-item-description' => '', 'menu-item-attr-title' => '', 'menu-item-target' => '', 'menu-item-classes' => '', 'menu-item-xfn' => '', 'menu-item-status' => '', );You should set the parent-id to the correct navigation item ID of the category. Using the post ID you can retrieve the URL and title of the news item and add it in the menu-item-title and menu-item-url fields.
The topic ‘Latest posts in menu’ is closed to new replies.