Search code examples
wordpress-themingcustom-post-typearchiveacfpro

How to make a custom Archive page with Wordpress using custom post types and acf fields


I am building a child theme on GeneratePress to create a custom site for a client and am stuck on customizing the Archive pages. I've determined that it will be best to ignore the theme functionality and build the page loops from scratch. The custom archive should take the place of wp/generatepress' standard archive page and function in the same way by showing relevant posts when a taxonomy term is clicked. I need to be able to choose which html tags are used and style them with css. My issue is that I don't understand wordpress dev enough to figure out how to do this.

I have two custom post types, Research and Resources, as well as a bunch of different custom taxonomies that are applied to both or each post type. I'd like to customize the archive page (with archive templates if necessary) so it displays the appropriate Research and/or Resource files when visited.

I understand that I need to loop through all the posts, but I do not know how to grab only the relevant ones per the given archive page.

Examples of loops with example content will be the most helpful. Thank you!!


Solution

  • I chatted with someone on Reddit who kindly answered this question for me here.

    They said:

    So I wouldn't do it as a template personally (you could), I would do it through having two new files a archive-research.php & archive-resources.php.

    Then if the loop is going to be the same on both pages, which I think you said it is going to be (you just want to display the posts from each custom post type), I would use a template part.

    So create a new folder in your theme folder called inc or includes and name the file custom_post_loop.php (name can be anything).

    Then in your archive page, call...

    <?php get_template_part('inc/custom_post_loop');?>

    In that file you are just wanting to write a simple post loop in there.

    while ( have_posts() ) : the_post();
        // Your loop code
            // just so you can see this work I have called the title
            the_title();
    
    
    endwhile;
    else : _e( 'Sorry, no posts were found.', 'textdomain' ); endif; ?>
    

    https://developer.wordpress.org/reference/functions/have_posts/

    Have a look at that link to find out more. But thats the direction to go...

    If you really wanted to got the direction you have with the template, you would have to add arguments to your posts loop.

    it works the same way but instead of archive.php its taxonomy.php...

    However, you don't want a taxonomy-{taxonomy name}.php file for each taxonomy as that would be ridiculous unless you knew all the taxonomies haha and had really specific user cases for them...

    so just create a taxonomy.php in your theme files and to make sure its working just add this code to it...

    <?php get_header();>
    
    <h1><?php the_archive_title();?></h1>
    
    //then put your loop in from earlier...
    
    <?php get_template_part('inc/custom_post_loop');?>
    
    <?php get_footer();?>