将CPT分类显示为存档页面

时间:2019-08-08 作者:Amor Armand

Hello Stack Exchange people,

I created a custom post type called articles. I also created a taxonomy city for that specific post type.

I can create a custom post and set a city to it and it gives me the url I\'m wishing to read this page.

https://www.example.com/blog/city_name/title_of_the_article/

The thing I wish to do is to create a city page that will act like the regular archive.php but just for the city of the custom post types.

For example, I created an article with a city set to "Tunis" so my URL is:

https://www.example.com/blog/tunis/my_article/

I would like this url: https://www.example.com/blog/tunis/ to be the archive page for the article with the city set to "Tunis".

I ran multiple tests but it didn\'t work (404 error).

For info, I don\'t have a archive-city.php page created.

Here is my code for CPT and the taxonomy. (For now and considering the code I\'m showing, the archive page is available at: https://www.example.com/blog/articles/)

register_post_type( \'article\', 
    array( \'labels\' => array(
        \'name\' => \'Articles\', 
        \'singular_name\' => \'Article\', 
        \'all_items\' => \'All article\', 
        \'add_new\' => \'Add article\', 
        \'add_new_item\' => \'Add article\',
        \'edit\' => \'Edit article\', 
        \'edit_item\' =>  \'Edit article\', 
        \'new_item\' => \'New article\', 
        \'view_item\' => \'View article\', 
        \'search_items\' => \'Search article\', 
        \'not_found\' =>  \'Nothing found in the Database.\', 
        \'not_found_in_trash\' => \'Nothing found in Trash\',
        \'parent_item_colon\' => \'\'
    ), 

    \'description\' => \'This is the example custom post type\', 
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'menu_position\' => 10, 
    \'menu_icon\' => \'dashicons-welcome-write-blog\', 
    \'rewrite\'   => array( \'slug\' => \'adresses\', \'with_front\' => true ), 
    \'has_archive\' => \'articles\', 
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'taxonomies\' => array( \'category\' ),

    \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\' ))); 

and

function resource_type() {
    $labels = array(
        \'name\'                       => _x( \'city\', \'Taxonomy General Name\', \'snt\' ),
        \'singular_name\'              => _x( \'city\', \'Taxonomy Singular Name\', \'snt\' ),
        \'menu_name\'                  => __( \'Add city\', \'snt\' ),
        \'all_items\'                  => __( \'All city\', \'snt\' ),
        \'parent_item\'                => __( \'Parent city\', \'snt\' ),
        \'parent_item_colon\'          => __( \'Parent city:\', \'snt\' ),
        \'new_item_name\'              => __( \'New city\', \'snt\' ),
        \'add_new_item\'               => __( \'Add New city\', \'snt\' ),
        \'edit_item\'                  => __( \'Edit city\', \'snt\' ),
        \'update_item\'                => __( \'Update city\', \'snt\' ),
        \'view_item\'                  => __( \'View city\', \'snt\' ),
        \'separate_items_with_commas\' => __( \'Separate items with commas\', \'snt\' ),
        \'add_or_remove_items\'        => __( \'Add or remove city\', \'snt\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'snt\' ),
        \'popular_items\'              => __( \'Popular city\', \'snt\' ),
        \'search_items\'               => __( \'Search city\', \'snt\' ),
        \'not_found\'                  => __( \'Not Found\', \'snt\' ),
        \'no_terms\'                   => __( \'No article\', \'snt\' ),
        \'items_list\'                 => __( \'Items list\', \'snt\' ),
        \'items_list_navigation\'      => __( \'Items list navigation\', \'snt\' ),
    );
    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => true,
        \'rewrite\'                    => array(\'slug\' => \'city\')
    );
    register_taxonomy( \'city\', array( \'article\' ), $args );
} 
add_action( \'init\', \'resource_type\', 0 );

To get https://www.example.com/blog/city/ as the archive page, what am I missing?

1 个回复
SO网友:Jacob Peattie

您正在描述默认行为。根据您的代码,您将拥有以下URL:

  • https://www.url.com/blog/addresses/ 所有文章的存档
  • https://www.url.com/blog/city/tunis/ 突尼斯城所有物品的档案
  • https://www.url.com/blog/addresses/article-title/ 一篇文章Settings>Permalinks页面就足够了(你甚至不需要按save,除非你在页面上做了更改)。

    至于要使用的模板:

    您可以看到模板层次结构的解释和可视化here.

    需要记住的一点是,这些模板都需要使用相同的main loop 与所有其他模板一样:

    <?php 
    if ( have_posts() ) : 
        while ( have_posts() ) : the_post(); 
            // Display post content
        endwhile; 
    endif; 
    ?>
    
    WordPress将自动查询此循环显示的正确帖子,因此您应该使用自定义WP_Query 查询这些模板中的帖子。

相关推荐