I realized that the_title();
can parse in file1.php outside of the loop, so why can’t the category and ID? The following solved my issue.
// file1.php
<div>
<h1><?php the_title(); ?></h2>
<?php
$category_array = wp_get_post_categories($post->ID);
$category_list = array();
foreach ( $category_array as $categories ) {
$category_list[] = get_cat_name( $categories );
}
$lister = implode(' • ', $category_list);
$list_categories = $lister;
echo $lister ;
?>
</div>
// file2.php
<article>
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; endif;
?>
</article>
Alternatively, to pass the category link, you could use the following (warning, changed variables below):
<ul>
<?php
// get post categories
$cats = wp_get_post_categories($post->ID);
foreach ( $cats as $category ) {
echo "<li>";
$current_cat = get_cat_name($category);
$cat_link = get_category_link($category);
echo "<a href="https://wordpress.stackexchange.com/questions/231734/$cat_link">";
echo $current_cat;
echo "</a>";
echo "</li>";
}
?>
</ul>