最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - Is it possible to add a dynamic link in the main navigation in a child theme

programmeradmin0浏览0评论

My client would like to have a link to the most recent post in the main navigation, but still be able to manage the navigation through the dashboard as usual. I did not find a plugin that will do this task, so I may need to custom code it. I could add it into the child theme, but I don't think it would should up in the dashboard menu where they could move it to a different order, if they want.

My searches are not yielding results in either Google or this Community, but I may just not be using the correct search term.

My client would like to have a link to the most recent post in the main navigation, but still be able to manage the navigation through the dashboard as usual. I did not find a plugin that will do this task, so I may need to custom code it. I could add it into the child theme, but I don't think it would should up in the dashboard menu where they could move it to a different order, if they want.

My searches are not yielding results in either Google or this Community, but I may just not be using the correct search term.

Share Improve this question asked Oct 18, 2020 at 21:01 Nora McDougall-CollinsNora McDougall-Collins 3952 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Create a new “Custom Link” menu item where the URL is #latestpost (which will act as a placeholder and target for the code). You can title the item whatever you please. This code will then find that menu item (by looking for the placeholder URL) and then replace it’s URL with that of the latest post’s URL. Since the filter is before the code that adds the selected CSS classes runs, the menu highlighting will even work. When you visit the latest post on your blog, this menu item will light up.

Place the code in a plugin or just in your theme’s functions.php file.

// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
    // Hook in early to modify the menu
    // This is before the CSS "selected" classes are calculated
    add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_latest_post', 10, 3 );
}
 
// Replaces a custom URL placeholder with the URL to the latest post
function replace_placeholder_nav_menu_item_with_latest_post( $items, $menu, $args ) {
 
    // Loop through the menu items looking for placeholder(s)
    foreach ( $items as $item ) {
 
        // Is this the placeholder we're looking for?
        if ( '#latestpost' != $item->url )
            continue;
 
        // Get the latest post
        $latestpost = get_posts( array(
            'numberposts' => 1,
        ) );
 
        if ( empty( $latestpost ) )
            continue;
 
        // Replace the placeholder with the real URL
        $item->url = get_permalink( $latestpost[0]->ID );
    }
 
    // Return the modified (or maybe unmodified) menu items array
    return $items;
}
发布评论

评论列表(0)

  1. 暂无评论