0

I am using advanced custom fields and want to display a page in the same layout like a category - with posts where the custom field has a specific value.

I have no clue how to approach this programmatically and do not know how to search for plugins helping to accomplish that.

Problems:

  • how to create a new page route? Or can I avoid this somehow and use the category templates for this and program the filter?

  • The page should be added to the menu - I probably would do this with a custom link?

Any help is appreciated.

1 Answer 1

1

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.

4
  • It seems like a good idea to use a custom taxonomy then.might be also easier to display then like a category?
    – Alex
    Commented Jun 15, 2017 at 16:02
  • 1
    Yes, I think 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
    – WebElaine
    Commented Jun 15, 2017 at 16:12
  • You might want to convert this to an answer :-)
    – Alex
    Commented Jun 16, 2017 at 10:36
  • 1
    Updated my original answer to offer this as the first suggestion. :)
    – WebElaine
    Commented Jun 16, 2017 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.