It's worth noting that querying by postmeta - which is what you'll have to do in order to identify all Posts that have a particular ACF field - can bog your website down.
Alternative: taxonomy
It would save you a lot of coding, be easier, and probably be faster for WP to load things if you use a taxonomy instead. To get you started in that direction: mondaybynoon.com/wordpress-archive-pages-taxonomy
Answer to original question
But, if you'd like to stick with custom fields, here's how you could approach it:
Changing the Page template
There are a couple of ways to get your Pages to appear like Categories.
If you want every Page you ever create to use custom fields and appear like a Category, then you'll want to create a custom page.php
in your theme.
If instead you may someday create a Page that you want to appear like a Page, then you'll want to create a custom tpl-page.php
in your theme.
Creating the template file
Whichever way you go, if you are not already using a custom theme, first create a child theme. That way whenever the parent theme updates, you will not lose your customizations. Basically, all you need to do here is create a style.css
file - add some minimal required comments so that WP will recognize the child theme, make sure to change the name so you can easily select the correct theme in the admin area, and keep your original theme installed as the parent theme. style.css
is all you technically need for WP to recognize that there's a new theme here.
Then, copy the parent theme's category.php
(or archive.php
if there is no category) contents and paste it into a new file - either page.php
or tpl-page.php
, whichever one you selected above. If you were already using a custom theme, you may edit page.php
directly or copy category.php
into a new file called tpl-page.php
.
Then, start modifying the code of your new file. How much you have to customize will depend on the theme you're working from.
Your new file should look something loosely like this:
<?php
// If you are editing tpl-page.php, make sure to add comments at the top
// so WP will recognize this custom Page Template. If not, remove the comment.
/*
* Template Name: Page as Category
*/
// Keep the header call and basic page markup here. Yours will vary.
get_header(); ?>
<main><?php
// Check for custom field
if(get_field('your_acf_field_requirement')) {
$customValue = get_field('your_acf_field_requirement');
// Query all posts that have this value.
$args = array(
'post_type' => 'post', // only grab Posts
'numberposts' => -1, // get all Posts that meet the requirements
'meta_key' => 'your_acf_meta_key_name', // check this meta key
'meta_value' => "$customValue" // match the Page's requirement
);
$posts = new WP_Query($args);
if($posts->have_posts()) {
while($posts->have_posts()) {
$query->the_post();
// Now you have the data. Display it as you like.
// If your theme already has template parts, use one.
// If not, you might manually need to display content like this:
?><div class="onePost"><?php
the_title();
?></div><?php
}
}
// reset
wp_reset_postdata();
}
// If you are editing page.php, you should have a fallback in case the field is empty:
else {
// For this part, keep the original page.php loop. Any pages where the
// ACF field is empty will just appear as normal Pages.
}
// Keep the footer call and basic page markup here. Yours will vary.
?></main>
<?php get_footer(); ?>
Once you get further along in development you can ask more specific questions about places you may be unsure of or stuck on.