Supongamos que tiene un menú generado por WordPress y desea vincular una de las entradas a la última publicación de su blog.
inserte este código en el archivo functions.php de su tema
// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
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;
}
ahora asegúrese de tener al menos un elemento de menú con un enlace que apunte a “#latestpost”: esa URL será sobrescrita por el último código.
(gracias a Alex http://www.viper007bond.com )