我不知道如何显示来自自定义post_type的自定义分类

时间:2020-08-25 作者:Tincho N

我创建了一个名为Events的自定义post\\u类型,可以在其中显示事件(URL:HTTP://localhost.com/events) 由存档事件加载的。我创建的php。

现在,我已经创建了一个名为counties的自定义分类法,我想用它来按事件所在的县过滤事件。我可以将县应用于特定的活动,但我不知道如何显示它们。我试图创建一个分类县。php

但我不知道如何测试或调试它,因为如果我尝试HTTP://localhost.com/events/countyHTTP://localhost.com/county 它带我到一个“404页未找到”。

这是我用来创建分类法的代码:

add_action(\'init\', \'create_counties_hierarchical_taxonomy\', 0);

function create_counties_hierarchical_taxonomy() {
    $labels = array(
    \'name\' => _x( \'Counties\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'County\', \'taxonomy singular name\' ),
    \'search_items\' => __( \'Search Counties\' ),
    \'all_items\' => __( \'All Counties\' ),
    \'parent_item\' => __( \'Parent County\' ),
    \'parent_item_colon\' => __( \'Parent County:\' ),
    \'edit_item\' => __( \'Edit County\' ), 
    \'update_item\' => __( \'Update County\' ),
    \'add_new_item\' => __( \'Add New County\' ),
    \'new_item_name\' => __( \'New County Name\' ),
    \'menu_name\' => __( \'Counties\' ),
    );
    
    // Register Taxonomy
    register_taxonomy(\'counties\',array(\'events\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'county\' ),
  ));
}
我曾尝试阅读WordPress开发人员文档,但不幸的是,我无法理解这一点,并找到了答案。

谢谢

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

好的,我为你工作过:

首先,我注册了自定义帖子类型和分类:

add_action(\'init\', \'my_init\');
add_action(\'admin_init\', \'my_admin_init\');

function my_admin_init() {
    $role = get_role( \'administrator\' );
  $role->add_cap( \'edit_event\' );
  $role->add_cap( \'read_event\' );
  $role->add_cap( \'delete_event\' );
  $role->add_cap( \'edit_events\' );
  $role->add_cap( \'read_events\' );
  $role->add_cap( \'delete_events\' );
  $role->add_cap( \'edit_others_events\' );
  $role->add_cap( \'publish_events\' );
  $role->add_cap( \'read_private_events\' );
  $role->add_cap( \'delete_private_events\' );
  $role->add_cap( \'delete_published_events\' );
  $role->add_cap( \'edit_private_events\' );
  $role->add_cap( \'edit_published_events\' );
  $role->add_cap( \'create_events\' );
  $role->add_cap( \'delete_others_events\' );
  $role->add_cap( \'delete_others_event\' );
}
function my_init() {
    global $post;
    $labels = array(
        \'name\'                => __( \'event\', \'webto\' ),
        \'singular_name\'       => __( \'Header component\', \'webto\' ),
        \'menu_name\'           => __( \'Event\', \'webto\' ),
        \'parent_item_colon\'   => __( \'Parental:\', \'webto\' ),
        \'all_items\'           => __( \'event\', \'webto\' ),
        \'view_item\'           => __( \'View\', \'webto\' ),
        \'add_new_item\'        => __( \'Add new event\', \'webto\' ),
        \'add_new\'             => __( \'Add new\', \'webto\' ),
        \'edit_item\'           => __( \'Edit event\', \'webto\' ),
        \'update_item\'         => __( \'Update event \', \'webto\' ),
        \'search_items\'        => __( \'Find event\', \'webto\' ),
        \'not_found\'           => __( \'Not found\', \'webto\' ),
        \'not_found_in_trash\'  => __( \'Not found in the bin\', \'webto\' ),
    );
    $args = array(
        \'labels\'              => $labels,
        \'supports\'            => array(\'title\'),
        \'public\'              => false,
        \'publicly_queryable\' => false,
        \'capability_type\' => array(\'event\', \'events\'),
        \'map_meta_cap\' => true,
        \'show_in_menu\' => true,
        \'rewrite\' => array( \'slug\' => \'events\' ),
        \'query_var\' => true,
        \'show_in_rest\' => false,
        \'show_ui\' => true,
        \'hierarchical\' => false,
        \'menu_position\'       => 1,
        \'menu_icon\'           => \'dashicons-welcome-widgets-menus\',
    );
    register_post_type( \'events\', $args );

    $labels = array(
        \'name\' => _x( \'county\', \'taxonomy general name\' ),
        \'singular_name\' => _x( \'county\', \'taxonomy singular name\' ),
        \'search_items\' => __( \'Search county\' ),
        \'all_items\' => __( \'All counties\' ),
        \'parent_item\' => __( \'Parent county\' ),
        \'parent_item_colon\' => __( \'Parent county:\' ),
        \'edit_item\' => __( \'Edit county\' ),
        \'update_item\' => __( \'Update county\' ),
        \'add_new_item\' => __( \'Add New county\' ),
        \'new_item_name\' => __( \'New county Name\' ),
        \'menu_name\' => __( \'counties\' )
    );

    // Register Taxonomy
    register_taxonomy(\'county\', array(\'events\'), array(
        \'hierarchical\' => true,
        \'public\'        => true,
        \'publicly_queryable\'        => true,
        \'labels\' => $labels,
        \'show_ui\' => true,
        \'show_admin_column\' => true,
        \'query_var\' => true
    ));
    flush_rewrite_rules();
}
我得到以下信息:

enter image description here

创建您的帖子并为其分配县

enter image description here

counties(县)页面上的counties(县)将具有可查看的链接

我们可以为特定的县创建存档页

taxonomy-county.php

同时显示您的帖子

// templates/your-template/taxonomy-county.php
<?php
global $post;
$county = get_queried_object();


$args = array(
  \'post_type\' => \'events\',
  \'numberposts\' => -1,
  \'tax_query\' => array(
    array(
      \'taxonomy\' => $county->taxonomy,
      \'field\' => \'term_id\',
      \'terms\' => $county->term_id
    )
  )
);
// Get posts by current county
$events = get_posts($args);

// All counties
$all_counties = get_terms($county->taxonomy);

foreach($all_counties as $county) {
    $args = array(
      \'post_type\' => \'events\',
      \'numberposts\' => -1,
      \'tax_query\' => array(
        array(
          \'taxonomy\' => $county->taxonomy,
          \'field\' => \'term_id\',
          \'terms\' => $county->term_id
        )
      )
    );
    // get posts by county
    $events = get_posts($args);
}

相关推荐

Child-theme breaks site

所以,我有一个子主题,里面除了所需的CSS文件之外什么都没有。一旦我激活了这个儿童主题,我的整个网站就关闭了。最后我有两个问题:激活一个只有CSS的子主题怎么能破坏我的网站</我怎样才能回到我原来的主题</这些是网站给我的错误:Warning: require_once(/wp-content/themes/interio_child/admin/options-framework.php) [function.require-once]: 无法打开流:中没有此类文件或目录/wp-c