最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Get posts from Custom Taxonomy

programmeradmin1浏览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 ...

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.

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 ...

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.

Share Improve this question edited Jul 30, 2019 at 9:20 Freddy asked Jul 30, 2019 at 8:50 FreddyFreddy 1771 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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',

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论