Conditional for post type inside loop (recent posts)

$post is a global variable, not a super global, you need to state that you’re intending to use it before doing so:

global $post;

But in this case, it would be easier to use:

get_post_type();

Some other notes:

  • You never do an if ( have_posts() ) check
  • There’s no cleanup call to wp_reset_postdata() which will cause problems for any use of the main query after this carousel
  • 'posts_per_page' => -1 could lead to some terrifying results, always set an upper bound, even if it’s a silly one you’ll never reach, such as 50 or 100, this way you don’t end up with that super laggy edge case. I would recommend 10, after all most people never go past the first slide, why would they look at the 11th?
  • Use echo or use print but stick to one, don’t mix and match
  • If a post has no featured image then it will be impossible to click through, use has_post_thumbnail and put a placeholder in if there isn’t one