You’ve wrapped the opening and closing of the while
in separate if
statements. This is the structure of your code.
if( $term ) {
// etc.
while ( $loop->have_posts() ) :
}
// etc.
if( $term ) {
endwhile;
}
This is not valid PHP. You need to structure it like this:
if( $term ) {
// etc.
while ( $loop->have_posts() ) :
// etc.
endwhile;
}