从自定义分类中获取帖子

时间:2019-07-30 作者:Freddy

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 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

您的第一块代码是正确的操作方式在任何情况下,存档模板都不应使用自定义WP_Query 用于查询其帖子WordPress根据URL为您查询正确的帖子,然后您使用标准WordPress循环输出它们:

while ( have_posts() ) : the_post();

endwhile; 
不过,有一些事情可能会造成问题。

首先,我注意到您的第一块代码Template Name 顶部块:

/**
* Template Name: Resources
*/
这只是为了使模板在编辑页面时显示为模板下的一个选项,但这不是自定义帖子类型存档的工作方式。

这就引出了下一个潜在的问题,即post类型本身。注册帖子类型时,其存档将在以下位置自动可用:/resources/, 不创建页面。正在创建archive-resources.php 只是告诉WordPress使用该模板,但如果您没有该模板,WordPress仍将加载正确的帖子,只会使用索引。而不是php。模板中对此进行了概述Hierarchy.

但是,要使其起作用,您的帖子类型必须是公共的,并且has_archive 不能为false:

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

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

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

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