Skip to content

Instantly share code, notes, and snippets.

@ivo-ivanov
Created September 23, 2019 13:44
Show Gist options
  • Save ivo-ivanov/9beac3b4f60a77f2f11313798658ecaf to your computer and use it in GitHub Desktop.
Save ivo-ivanov/9beac3b4f60a77f2f11313798658ecaf to your computer and use it in GitHub Desktop.
Loop specific WordPress Menu and display content. #wordpress
<?php
$nav_items = wp_get_nav_menu_items( 'main', array( // name of the specific menu
'order' => 'ASC', // List ASCending or DESCending
'orderby' => 'title', // Order by your usual, menu_order, post_title, etc. Check WP_Query
'post_type' => 'nav_menu_item', // To be honest, I'm not sure why this is an option, leave it be.
'post_status' => 'publish', // If there are private / draft posts in our menu, don't show them
'output' => ARRAY_A, // Return an Array of Objects
'output_key' => 'menu_order', // Not sure what this does
'nopaging' => true, // Not sure what this does
'update_post_term_cache' => false // Not sure what this does
) );
$index = 1;
if( ! empty( $nav_items ) ) {
foreach( $nav_items as $item ) {
$thepageid = $item->object_id;
$post = get_post($thepageid); // specific post
$the_content = apply_filters('the_content', $post->post_content);
if ( !empty($the_content) ) {
echo '<div id="page-'. $index++ .'">'. $the_content .'</div>';
}
}
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment