Looking at your code, my first question is, what is the value of
$page_id. It’s not defined. On this line
have_rows('tab_content', $page_id) ) since it is not defined then ACF is assuming the current post ID, whatever that might be.
get_sub_field('tab_image', $page_id); for sub fields you do not need to specify the post ID. This second parameter is whether or not to format the value. Since
$page_id is undefined at this point, or null, that equals
false and this is causing ACF to return the unformatted value of the sub field, in this case the attachment ID.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!