Wordpress自定义post_type归档网址和默认查询

时间:2015-06-03 03:32:01

标签: php wordpress templates themes custom-post-type

我现在正在使用wordpress 4.2,并为我自己的网站开发主题。

我在主题中定义了许多自定义帖子类型,现在我正在寻找一种方法为它们制作一些存档页面。

例如,我有一个名为shop的post_type,并为其分配了一个名为region的分类。

我创建了一个模板文件:category-region.php

然后我访问此模板的固定链接:http://example.com/category/region/,此模板已匹配,并在所有已分配的商店中启动查询,然后显示它们。

但现在我想创建一个包含所有商店帖子的页面,无论他们分配到哪个区域。

我应该如何命名模板文件以及永久链接?

我检查了模板层次结构并找到了下图。

https://developer.wordpress.org/themes/basics/template-hierarchy/

enter image description here

似乎archive-$post_type.php是我想要的。

但是指向这个模板的固定链接呢?

1 个答案:

答案 0 :(得分:2)

您传递给rewrite的参数中的register_post_type参数决定了自定义帖子类型存档的固定链接。

https://codex.wordpress.org/Function_Reference/register_post_type

查看文档中的示例:

$args = array(
    'labels'             => $labels,
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'book' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);

register_post_type( 'book', $args );

您可以看到图书CPT的重写段塞为book,因此图书档案的网址为example.com/book/

在您的情况下,如果您将重写slug设置为shop(或者如果您没有设置重写slug,在这种情况下它将默认为您的CPT slug),那么对应的URL您的archive-shop.php模板将为example.com/shop/

请注意,这假定您使用固定链接,如果您未使用永久链接,则商店帖子类型存档位于example.com/?post_type=shop。如果您使用固定链接,请务必在设置CPT后通过访问Settings > Permalinks页面刷新它们。