列出CPT所有类别的页面

时间:2017-05-22 作者:MikeiLL

我有一个名为“服务”的自定义帖子类型,其中有一个名为“service\\u areas”的类别,我希望能够打开一个列出所有“service\\u areas”的页面。我不太确定这个页面的名称,因为我认为“分类归档页面”是一个列出某个特定“service\\u领域”中所有帖子的页面。

调用urldomain-name.com/service_areas/a_service_categorya_service_category 按预期分类。

什么是制作的好方法domain-name.com/service_areas 调出一个页面,列出所有“service\\u区域”(与404相对)?

更多详细信息:

自定义帖子类型为services 它的自定义分类是services_category 带着一点service_areas:

/*
 * Create Services CPT
 */
function services_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Services\', \'Post Type General Name\', \'total\' ),
        \'singular_name\'       => _x( \'Service\', \'Post Type Singular Name\', \'total\' ),
        \'menu_name\'           => __( \'Services\', \'total\' ),
        \'parent_item_colon\'   => __( \'Parent Service\', \'total\' ),
        \'all_items\'           => __( \'All Services\', \'total\' ),
        \'view_item\'           => __( \'View Service\', \'total\' ),
        \'add_new_item\'        => __( \'Add New Service\', \'total\' ),
        \'add_new\'             => __( \'Add New\', \'total\' ),
        \'edit_item\'           => __( \'Edit Service\', \'total\' ),
        \'update_item\'         => __( \'Update Service\', \'total\' ),
        \'search_items\'        => __( \'Search Service\', \'total\' ),
        \'not_found\'           => __( \'Not Found\', \'total\' ),
        \'not_found_in_trash\'  => __( \'Not found in Trash\', \'total\' ),
    );


    $args = array(
        \'label\'               => __( \'services\', \'total\' ),
        \'description\'         => __( \'Services we offer\', \'total\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'revisions\' ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_icon\' => \'dashicons-businessman\',
        \'menu_position\'       => 20,
        \'can_export\'          => true,
        \'rewrite\'            => array( \'slug\' => \'services\' ),
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
    );

    // Registering your Custom Post Type
    register_post_type( \'services\', $args );

}



/**
 * Register Caregiver category.
 *
 */
function register_categories() {

    // Define and sanitize options
    $name = __( \'Service Categories\', \'total\' );
    $slug = \'service_areas\';

$args = array(
    \'labels\' => array(
        \'name\' => $name,
        \'singular_name\' => $name,
        \'menu_name\' => $name,
        \'search_items\' => __( \'Search\',\'total\' ),
        \'popular_items\' => __( \'Popular\', \'total\' ),
        \'all_items\' => __( \'All\', \'total\' ),
        \'parent_item\' => __( \'Parent\', \'total\' ),
        \'parent_item_colon\' => __( \'Parent\', \'total\' ),
        \'edit_item\' => __( \'Edit\', \'total\' ),
        \'update_item\' => __( \'Update\', \'total\' ),
        \'add_new_item\' => __( \'Add New\', \'total\' ),
        \'new_item_name\' => __( \'New\', \'total\' ),
        \'separate_items_with_commas\' => __( \'Separate with commas\', \'total\' ),
        \'add_or_remove_items\' => __( \'Add or remove\', \'total\' ),
        \'choose_from_most_used\' => __( \'Choose from the most used\', \'total\' ),
    ),
    \'public\' => true,
    \'show_in_nav_menus\' => true,
    \'show_ui\' => true,
    \'show_tagcloud\' => true,
    \'hierarchical\' => true,
    \'rewrite\' => array( \'slug\' => $slug, \'with_front\' => false ),
    \'query_var\' => true
);

    register_taxonomy( \'services_category\', array( \'services\' ), $args );

}

add_action( \'init\', \'services_post_type\', 0 );
add_action( \'init\', \'register_categories\', 0 );
==================

我注意到如果我过滤“pre\\u get\\u posts”,$query->query[\'name\'] == \'service_areas\' 对于该url为true。我可能会加载一个基于该测试的自定义模板并循环通过get_terms( \'service_areas\' ), 但我不知道怎么做。

1 个回复
最合适的回答,由SO网友:eballeste 整理而成

我会在管理面板中创建一个名为服务区域的页面,并创建一个重写规则,告诉wp加载相应的page-service-areas.php 样板

function add_rewrite_rules () {
    add_rewrite_rule( \'^service_areas/?$\', \'index.php?pagename=service-areas\', \'top\' );
}
add_action(\'init\', \'add_rewrite_rules\');
然后在页面服务区域。php构建您想要的查询。

以下链接可帮助您可视化wordpress如何确定要加载的页面模板:

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

更新时间:

添加上述功能(主题functions.php orelsewhere)永久链接”刷新重写规则,创建一个名为“服务区域”的空白页面现在,您应该可以单击在admin中创建的页面的链接(yoursite.com/service-areas) 还是因为我们的重写规则-(yoursite.com/service_areas) 并查看模板页面。

结束

相关推荐