0

I've registered a new post type called Resources.

Within the theme root, I've created archive-resources.php which looks like this:

<?php 

/**
* Template Name: Resources
*/

get_header();

    while ( have_posts() ) : the_post(); ?>
        <div class="resource__wrapper">
            <?php the_content(); ?>
        </div>

    <?php
    endwhile; 
    wp_reset_query(); 

get_footer(); ?>

Within Resources, I have a test post called post 1 (which is published). I would've expected the current archive-resources.php to print out post 1? But it just prints out "Welcome to WordPress...".

I've also tried the following:

Approach 1:

<?php 

/**
* Template Name: Resources Level 1
*/

get_header();

    while ( have_posts() ) : the_post();
        $args = array(
          'post_type' => 'resources',
          'posts_per_page' => 10,
          'showposts' => 10,
          'post_status' => 'publish',
          'orderby' => 'publish_date',
          'order' => 'DESC'
        );

        $the_query = new WP_Query($args);
        $count = 1;

        if ( $the_query->have_posts() ) {

            while ($the_query->have_posts() ) {
                $the_query->the_post();

                get_template_part('templates/widgets/resource-card');

                $count++;

                if($count > 10) { 
                    $count = 1; 
                }
            }

        }
    endwhile; 

    wp_reset_query(); 

get_footer(); ?>

?>

And resource-card.php is:

<div id="resource-card" class="col-12 col-sm-6" >

  <div class="resourceCard__wrapper">
    <h3 class="resourceCard__title"></h3><?php echo get_bloginfo( 'name' ); ?></h3>
    <p class="resourceCard__subtitle"> </p>
  </div>

</div>

Approach 2:

<?php 

/**
* Template Name: Resources Level 1
*/

get_header();


$args = array(
  'post_type' => 'resources',
  'post_status' => 'publish',
  'orderby' => 'publish_date',
  'order' => 'DESC'
);              

$the_query = new WP_Query( $args );

if($the_query->have_posts()) : 
    while($the_query->have_posts()) : $the_query->the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <?php the_content();

    endwhile;
endif; 

wp_reset_query();

get_footer(); ?>

?>

Where am I going wrong?

Update:

Just noticed under posts that there's a Hello World post (which is why it's appearing on my resources page).

So it seems like it isn't looking for posts in the Resources type ...

enter image description here

Current Approach:

archive-resource.php

<?php 

get_header();

    while ( have_posts() ) : the_post(); ?>
        <div class="resource__wrapper">
            <?php the_content(); ?>
        </div>

    <?php
    endwhile; 
    wp_reset_query(); 

get_footer(); ?>

?>

How I'm registering the taxonomy:

public function post_types_taxonomies() {
    register_post_type(
        'resources',
        build_post_args(
            'resources', 'Resource', 'Resources',
            array(
                'menu_icon'     => 'dashicons-welcome-write-blog',
                'menu_position' => 20,
                'has_archive'   => true,
                'public'      => true
            )
        )
    );
}

In WP Admin, under Settings > Reading > Posts page is set to Resources

I have a page called Resources (/resources).

... Still not showing post titled post 1. I believe index.php is taking over since the default "welcome to WordPress ..." blog is still being shown.

1 Answer 1

1

Your very first block of code is the correct way to do things. Under no circumstances should an archive template be using a custom WP_Query for querying its posts. WordPress queries the correct posts for you based on the URL, and you output them with the standard WordPress Loop:

while ( have_posts() ) : the_post();

endwhile; 

There's a few things that could be causing problems though.

Firstly, I noticed though is that your first block of code has the Template Name block at the top:

/**
* Template Name: Resources
*/

This is only supposed to be used so that the template appears as an option under Template when editing a page, but that's not how custom post type archives are supposed to work.

That brings me to the next potential issue, which is the post type itself. When you register a post type, its archives will be automatically available at /resources/, without creating a page. Creating archive-resources.php just tells WordPress to use that template, but if you don't have that template WordPress will still load the correct posts, only it will be using index.php instead. This is outlined in the Template Hierarchy.

For this to work though, your post type needs to be public, and has_archive cannot be false:

// OK
'public' => true,

// NOT OK
'public'      => true,
'has_archive' => false,

// OK
'public'      => true,
'has_archive' => true,

// OK
'public'      => true,
'has_archive' => 'resources',
6
  • Hi Jacob, I've had a look at the hierarchy and (even after applying the changes suggested), I believe index.php is still being executed over archive-resources.php. I've updated my question to showcase how I've registered the taxonomy, seems okay to me, but maybe the issue lies there?
    – Freddy
    Commented Jul 30, 2019 at 9:25
  • I just noticed in your question that you have a page called Resources, and this is the Posts page in Settings > Reading. This will replace that page with your blog, and conflict with your post type. You can't also have a page with that name. Commented Jul 30, 2019 at 9:29
  • ah, my assumption was that defining it in settings would essentially make it the "index" for that blog. Nevertheless, seems like something has happened. So, I've removed it as the Posts page from settings (the Resources page still exists), I don't see the "Hello WordPress" post anymore on /resources (which is good), but I still don't see my post under Resources taxonomy appearing on the page?
    – Freddy
    Commented Jul 30, 2019 at 9:37
  • As I said, you can't also have a Resources page. At least not with that slug. Commented Jul 30, 2019 at 9:41
  • Your use of the word "also" suggested only one of them needed removing. So, 1. I've completely removed the Resources page from pages. 2. As a result of this, Posts page in settings is now --Select-- (since the Resource page doesn't exist anymore, I can't select it anyway). 3. I would assume register_post_type would've defined the resources URL? Because when I now go on /resources, it's page not found
    – Freddy
    Commented Jul 30, 2019 at 9:50

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.