Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1404350

    Dear Enfold-Team,

    is there any way to link to the latest portfolio of a certain category? For example: I want to add a menu item with this link.

    Best
    biocomag

    #1404559

    Hey Hans,
    Thanks for your patience, I found this article: Add A Link To Latest Post To WordPress Nav Menu and it works for Posts.
    I was able to adjust it to show the latest portfolio, but not for a specific category in the portfolio:
    Try adding this code to the end of your child theme functions.php file in Appearance ▸ Editor:

    // 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,
    			'post_type' => 'portfolio',
    			//'category' => 1, //only works for posts if you comment out the post_type line above, portfolio_entries & taxonomy doesn't work either
            ) );
     
            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;
    }

    and create a custom menu item with the URL #latestpost
    Enfold_Support_1765.jpeg

    Best regards,
    Mike

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Link to latest portfolio’ is closed to new replies.