自定义帖子类型的问题

时间:2017-07-28 作者:Marcos Di Paolo

我最近创建了一个名为events的自定义帖子类型,我打算在这里创建包含我所在区域的艺术活动的帖子。我如何访问站点中的这些帖子?因为如果我去:localhost:####/events 它引导我找到索引。php,既不到cpt归档文件,也不到cpt post列表(使用localhost:####/[ctp post-title] 我得到个人帖子)。

此外,添加类别也很棘手,当我看到帖子时,类别没有列出。

另外,我可以从RESTAPI访问这些帖子吗?我是说从wp-json/wp/v2/posts

这里是整个功能。带有分类法和列setu的php代码:

// 1. Custom Post Type Registration (Events)

add_action( \'init\', \'create_event_postype\' );

function create_event_postype() {

$labels = array(
    \'name\' => _x(\'Agenda\', \'post type general name\'),
    \'singular_name\' => _x(\'Agenda\', \'post type singular name\'),
    \'add_new\' => _x(\'Agregar nuevo\', \'events\'),
    \'add_new_item\' => __(\'Agregar nuevo evento\'),
    \'edit_item\' => __(\'Editar evento\'),
    \'new_item\' => __(\'Nuevo evento\'),
    \'view_item\' => __(\'Ver evento\'),
    \'search_items\' => __(\'Buscar eventos\'),
    \'not_found\' =>  __(\'No se encuentran eventos\'),
    \'not_found_in_trash\' => __(\'No hay eventos en la basura\'),
    \'parent_item_colon\' => \'\',
);

$args = array(
    \'label\' => __(\'Agenda\'),
    \'labels\' => $labels,
    \'public\' => true,
    \'can_export\' => true,
    \'show_ui\' => true,
    \'_builtin\' => false,
    \'capability_type\' => \'post\',
    \'menu_icon\' => get_bloginfo(\'template_url\').\'/images/cal.png\',
    \'hierarchical\' => false,
    \'rewrite\' => array( "slug" => "events" ),
    \'supports\'=> array(\'title\', \'thumbnail\', \'excerpt\', \'editor\') ,
    \'show_in_nav_menus\' => true,
    \'taxonomies\' => array( \'eventcategory\', \'post_tag\')
);

register_post_type( \'events\', $args);

}

// 2. Custom Taxonomy

function create_eventcategory_taxonomy() {

$labels = array(
    \'name\' => _x( \'Categories\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Category\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Categories\' ),
    \'popular_items\' => __( \'Popular Categories\' ),
    \'all_items\' => __( \'All Categories\' ),
    \'parent_item\' => null,
    \'parent_item_colon\' => null,
    \'edit_item\' => __( \'Edit Category\' ),
    \'update_item\' => __( \'Update Category\' ),
    \'add_new_item\' => __( \'Add New Category\' ),
    \'new_item_name\' => __( \'New Category Name\' ),
    \'separate_items_with_commas\' => __( \'Separate categories with commas\' ),
    \'add_or_remove_items\' => __( \'Add or remove categories\' ),
    \'choose_from_most_used\' => __( \'Choose from the most used categories\' ),
);

register_taxonomy(\'eventcategory\',\'events\', array(
    \'label\' => __(\'Event Category\'),
    \'labels\' => $labels,
    \'hierarchical\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'event-category\' ),
));
}

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

// 3. Show Columns

add_filter ("manage_edit-events_columns", "events_edit_columns");
add_action ("manage_posts_custom_column", "events_custom_columns");

function events_edit_columns($columns) {

$columns = array(
    "cb" => "<input type=\\"checkbox\\" />",
    "col_ev_cat" => "Categoría",
    "col_ev_date" => "Fecha",
    "col_ev_times" => "Hora",
    "col_ev_thumb" => "Foto",
    "title" => "Evento",
    "col_ev_desc" => "Descripción",
    );
return $columns;
}

function events_custom_columns($column)
{
global $post;
$custom = get_post_custom();
switch ($column)
{
case "col_ev_cat":
    // - show taxonomy terms -
    $eventcats = get_the_terms($post->ID, "eventcategory");
    $eventcats_html = array();
    if ($eventcats) {
    foreach ($eventcats as $eventcat)
    array_push($eventcats_html, $eventcat->name);
    echo implode($eventcats_html, ", ");
    } else {
    _e(\'None\', \'themeforce\');;
    }
break;
case "col_ev_date":
    // - show dates -
    $startd = $custom["events_startdate"][0];
    $endd = $custom["events_enddate"][0];
    $startdate = date("F j, Y", $startd);
    $enddate = date("F j, Y", $endd);
    echo $startdate . \'<br /><em>\' . $enddate . \'</em>\';
break;
case "col_ev_times":
    // - show times -
    $startt = $custom["events_startdate"][0];
    $endt = $custom["events_enddate"][0];
    $time_format = get_option(\'time_format\');
    $starttime = date($time_format, $startt);
    $endtime = date($time_format, $endt);
    echo $starttime . \' - \' .$endtime;
break;
case "col_ev_thumb":
    // - show thumb -
    $post_image_id = get_post_thumbnail_id(get_the_ID());
    if ($post_image_id) {
    $thumbnail = wp_get_attachment_image_src( $post_image_id, \'post-thumbnail\', false);
    if ($thumbnail) (string)$thumbnail = $thumbnail[0];
    echo \'<img src="\';
    echo bloginfo(\'template_url\');
    echo \'/timthumb/timthumb.php?src=\';
    echo $thumbnail;
    echo \'&h=60&w=60&zc=1" alt="" />\';
}
break;
case "col_ev_desc";
    the_excerpt();
break;

}
}

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

在您的$args 数组,添加\'show_in_rest\' => true,. 这将启用REST API集成。

对于URL,需要刷新重写规则。通常情况下,您会希望将自定义帖子类型/分类定义放入插件中,这样它就可以与主题分离,因为它处理的是内容创建/结构,而不是显示。你可以打电话给flush_rewrite_rules() 使用插件激活/停用挂钩。

在您的情况下,只需进入永久链接设置并保存它们(如果您将其保留在主题中)。

结束

相关推荐