Multiple Loops Inside a Function

the permalink and post title won’t be available until after you’ve run the query. the following should return the last 3 posts from the designated category.

the same post is being displayed 3 times because it doesn’t look like you’re setting the permalink or post title from within “the loop”

if (!function_exists('get_posts_menus')):
  function get_posts_menus($category_name, $count)
{

  $args = array(
    'category_name' => $category_name,
    'posts_per_page' => $count
    );

  $the_menu = new WP_Query($args);

  while ($the_menu->have_posts()) {
    $post_class = get_the_id();
    $permalink = get_the_permalink();
    $title     = get_the_title();
    $the_menu->the_post();
    echo "<li class=\"post-id-$post_class\"><a href=\"$permalink\" alt=\"$title\">$title</a></li>";
  }
    wp_reset_postdata();

  }
  endif;