0

Developing my first custom theme and I started out using an individual page-$slug.php to display a list of each custom post. Have since learned that archive.php is better for this purpose (but I just want to use one if possible).

So in admin I've changed the links to the 'post type archive' instead of the page I'd created, and set has_archive to true for each cpt.

But the link now takes me to index.php

My archive.php so far is just this:

<?php
get_header();

if (have_posts()):
    while (have_posts()): the_post();
        the_title();
        the_content();
    endwhile;
endif;

get_footer();
?>

How do I get it do use the archive.php template and output just the posts of the requested type?

Thanks in advance!

Edit:

Permalinks are set to 'post type'

Here's the code for 'resource'

register_post_type('resource', array(
        'public' => true,
        'has_archive' => true,
        'taxonomies'  => array(
            'category'),
        'supports' => array(
            'title', 
            'editor', 
            'excerpt',
            'page-attributes',
            'custom-fields'),
        'labels' => array(
            'name' => 'Resources',
            'add_new_item' => 'Add New Resource',
            'edit_item' => 'Edit Resource',
            'all_items' => 'All Resources',
            'singular_name' => 'Resource')
    ));

and a URL for the archive link is http://youth-cafe.local/resource/

  • 1
    Are permalinks turned on? What does Query Monitor say in the template hierarchy section? Can you give us an example of a URL and post type? – Tom J Nowell Apr 3 at 13:07
  • Thanks Pratik, however it seems to have started working..... it's suddenly going to archive.php. This may be something to do with Flywheel(?), this has happened previously where changes don't register immediately. I tried rebooting a couple of times but nothing happened until now. I'll add the requested examples anyway. – JakePowell Apr 3 at 13:21
  • 2
    Sounds like you're dealing with caching of some sort – Tom J Nowell Apr 3 at 13:52
  • 1
    You can disable caching on Flywheel by turning on Development Mode under the Advanced tab. – MikeNGarrett Apr 3 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.