Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active November 22, 2021 09:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/a78a00d239d7304d264716cf53db1855 to your computer and use it in GitHub Desktop.
Save devinsays/a78a00d239d7304d264716cf53db1855 to your computer and use it in GitHub Desktop.
Displays posts based on what has been added to a menu location
<?php
$menu = 'featured-content'; // Replace with your menu name
if ( has_nav_menu( $menu ) ) :
// Get post ids of items in the menu
$items = wp_get_nav_menu_items( $menu );
foreach ( $items as $item ) {
$ids[] = get_post_meta( $item->ID, '_menu_item_object_id', true );
}
// var_dump( $ids );
if ( isset( $ids ) ) :
$args = array(
'posts_per_page' => 6, // # of posts to appear
'post_type' => array( 'post', 'page', 'guide' ), // Post types
'post__in' => $ids,
'orderby' => 'post__in'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="article">
<a href="<?php the_permalink(); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</a>
</div>
<?php endwhile;
endif;
wp_reset_query();
endif;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment