Skip to content

Instantly share code, notes, and snippets.

@landbryo
Created June 13, 2019 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landbryo/12cb584a4cd473332c52a33bddb1ed95 to your computer and use it in GitHub Desktop.
Save landbryo/12cb584a4cd473332c52a33bddb1ed95 to your computer and use it in GitHub Desktop.
This function looks for the most recent post and adds it to any menu item with #placeholder as a link.
/**
* Look for placeholder url and replace it with latest post
*
* @param $items
*
* @return mixed
*/
function latest_conference_item( $items ) {
// Get the latest conference
$latestpost = get_posts( array(
'numberposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'post_type' => 'post'
) );
foreach ( $items as $item ) {
if ( empty( $latestpost ) || '#placeholder' != $item->url ) {
continue;
}
// Replace the placeholder with the real URL
$item->url = get_permalink( $latestpost[0]->ID ) . 'program';
}
return $items;
}
if ( ! is_admin() ) {
add_filter( 'wp_get_nav_menu_items', 'latest_conference_item', 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment