How can I loop through the fields and display the content without having to deplicate it?
This for loop will cycle through the four field names to avoid duplication.
<?php
for ($i=1; $i<=4; $i++) {
$fieldName="featured_content_" . $i;
$featuredContentPostID = get_field($fieldName)->ID;
$selectedFeaturedContent = get_post( $featuredContentPostID );
?>
<li>
<a href="https://wordpress.stackexchange.com/questions/204839/<?php echo esc_url( get_permalink($featuredContentPostID) ); ?>">
<?php echo get_the_post_thumbnail( $featuredContentPostID, 'thumbnail' ); ?>
<span><?php echo $selectedFeaturedContent->post_title ?></span>
</a>
</li>
<?php
}
?>
Is it best practice to name them the same? i.e “featured_content”
Normally, I think you’re better off with more specific names – however if the plugin is adding a more “generic” feature (i.e. allowing the user to feature four arbitrary bits of content like this) then this naming scheme is fine.
One last question – how could I put a flag on one of them from the custom field section so I could promote one of them?
I’d add another custom field, perhaps a drop-down named something like promoted_content_field
. There, the user can choose which of the four to promote (or none). Then, in the loop above just add a check to see if the field that’s currently being processed is the promoted one chosen in promoted_content_field