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

Not able to see archive page of custom posts

programmeradmin3浏览0评论

I am using Wordpress 5.4. I created a CPT using the code below:

$args = array(
    'labels'        => $labels,
    'description'   => 'Some description',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
register_post_type( 'article', $args ); 

has_archive was set to true and my understanding is that now when I goto /articles I am supposed to see all posts of type article. But that is not the case. When I goto /articles I see my normal posts. I do not have any article-archive.php on my system and understand that is not necessary. I do have a ./wp-content/themes/twentynineteen/archive.php and I am using a child theme of twnetynineteen on my site.

How can I fix this?

EDIT:

  1. I have tried /article and it does not fix the problem.
  2. I have tried resaving permalinks (Settings->Permalinks) and it does not fix the problem.
  3. The code above is located in a plugin.
  4. I can see my article if I goto /?article=test-article where test-article is slug of my article.
  5. I am using nginx with this configuration.

I am using Wordpress 5.4. I created a CPT using the code below:

$args = array(
    'labels'        => $labels,
    'description'   => 'Some description',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
register_post_type( 'article', $args ); 

has_archive was set to true and my understanding is that now when I goto /articles I am supposed to see all posts of type article. But that is not the case. When I goto /articles I see my normal posts. I do not have any article-archive.php on my system and understand that is not necessary. I do have a ./wp-content/themes/twentynineteen/archive.php and I am using a child theme of twnetynineteen on my site.

How can I fix this?

EDIT:

  1. I have tried /article and it does not fix the problem.
  2. I have tried resaving permalinks (Settings->Permalinks) and it does not fix the problem.
  3. The code above is located in a plugin.
  4. I can see my article if I goto /?article=test-article where test-article is slug of my article.
  5. I am using nginx with this configuration.
Share Improve this question edited Apr 13, 2020 at 16:18 morpheus asked Apr 12, 2020 at 20:06 morpheusmorpheus 1557 bronze badges 4
  • technically the archive slug should be /article/ not /articles/. The default archive.php file should be able to display your articles. – Shazzad Commented Apr 12, 2020 at 20:16
  • /article also does not work – morpheus Commented Apr 12, 2020 at 21:12
  • Where is the code in your question located? Do permalinks work for other post types and pages? What are your permalinks set to? – Tom J Nowell Commented Apr 13, 2020 at 16:06
  • Permalinks are set to /?p=123 in /wp-admin/options-permalink.php – morpheus Commented Apr 13, 2020 at 16:20
Add a comment  | 

2 Answers 2

Reset to default 1

has_archive was set to true and my understanding is that now when I goto /articles I am supposed to see all posts of type article.

Nowhere in your code is articleS mentioned, the slug of the post type is article, so the URL of its archive should be /article/. If you want it to be articles you have to say so.

In this case, it's the classic mistake of registering a post type or taxonomy, but not flushing permalinks to add their rules. Go to the permalinks setting page, and resave.

As for the template archive.php, this is irrelevant to wether the page loads or not. The template hierarchy will fallback to index.php which is required for a theme if no other templats are found. Additionally, archive.php doesn't make the URL an archive. The URL makes it an archive, and the fact it's an archive makes WP load that template. WP has already figured out what posts to fetch before the template is chosen.

The solution to this problem is to use Post name in /wp-admin/options-permalink.php.

发布评论

评论列表(0)

  1. 暂无评论