多个自定义帖子类型使用相同的模板

时间:2016-01-06 07:51:46

标签: php wordpress

我有三个自定义帖子类型使用相同的模板:

// Custom posttype Photos
$labels = array(
    'name' => _x('Photos', 'Post Type General Name'),
    'singular_name' => _x('Photos', 'Post Type Singular Name'),
    'menu_name' => __('Photos'),
    'parent_item_colon' => __('Photos:'),
    'all_items' => __('All Items'),
    'view_item' => __('View Item'),
    'add_new_item' => __('Add New Event'),
    'add_new' => __('Add New'),
    'edit_item' => __('Edit Item'),
    'update_item' => __('Update Item'),
    'search_items' => __('Search Item'),
    'not_found' => __('Not found'),
    'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
    'labels' => $labels,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
    'taxonomies' => array('post_tag'),
    'hierarchical' => false,
    'rewrite' => array('slug' => 'photos'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 10,
    'menu_icon' => 'dashicons-format-image',
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'post',
);
register_post_type('photos', $args);

// Custom posttype Events
$labels = array(
    'name' => _x('Events', 'Post Type General Name'),
    'singular_name' => _x('Events', 'Post Type Singular Name'),
    'menu_name' => __('Events'),
    'parent_item_colon' => __('Events:'),
    'all_items' => __('All Items'),
    'view_item' => __('View Item'),
    'add_new_item' => __('Add New Event'),
    'add_new' => __('Add New'),
    'edit_item' => __('Edit Item'),
    'update_item' => __('Update Item'),
    'search_items' => __('Search Item'),
    'not_found' => __('Not found'),
    'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
    'labels' => $labels,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
    'taxonomies' => array('post_tag'),
    'hierarchical' => false,
    'rewrite' => array('slug' => __('events')),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 10,
    'menu_icon' => 'dashicons-images-alt2',
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'post',
);
register_post_type('events', $args);

// Custom Courses

    $labels = array(
    'name' => _x('Courses', 'Post Type General Name'),
    'singular_name' => _x('Courses', 'Post Type Singular Name'),
    'menu_name' => __('Courses'),
    'parent_item_colon' => __('Courses:'),
    'all_items' => __('All Items'),
    'view_item' => __('View Item'),
    'add_new_item' => __('Add New Item'),
    'add_new' => __('Add New'),
    'edit_item' => __('Edit Item'),
    'update_item' => __('Update Item'),
    'search_items' => __('Search Item'),
    'not_found' => __('Not found'),
    'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
    'labels' => $labels,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
    'taxonomies' => array('post_tag'),
    'hierarchical' => false,
    'rewrite' => array('slug' => 'courses'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 10,
    'menu_icon' => 'dashicons-welcome-learn-more',
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'capability_type' => 'post',
);
register_post_type('courses', $args);

我需要3个页面(或归档/分类法?):活动显示所有帖子活动,页面课程显示所有帖子课程,页面照片显示所有帖子照片(他们使用相同的前端和左侧边栏(有功能搜索)和正确的内容)

目前我使用模板Left Sibar 3页事件,课程,照片:

/*
* Template Name: Left Sidebar
*/

我想在每个页面中显示帖子类型的名称,并获取帖子类型的所有帖子。例如:

  • 页面事件:显示字符串'事件列表'和所有事件
  • 页面课程:显示字符串“课程列表”和所有课程
  • 页面照片:显示字符串'照片列表'和所有照片

但是这个模板无法获得帖子类型信息:(

另一种方式,我使用hierachy wordpress自定义帖子类型:

  • 发布类型事件:archive-events.php
  • 发布照片类型:archive-photos.php
  • 发布类型课程:archive-courses.php

为什么找不到“http://mydomain.local/events/”? (事件是帖子类型事件的slu))。其他人一样。

有关此问题的任何想法吗?

如果您需要更多信息,请发表评论。抱歉我的英语不好^^

1 个答案:

答案 0 :(得分:0)

在每个$args数组中,您都将has_archive设置为false。这应该设置为' true'。仅此一点就可以帮助您顺利完成任务。然后,域http://mydomain.local/events/应该有效。查看' has_archive'的文档。在https://codex.wordpress.org/Function_Reference/register_post_type进行确认。

本指南:http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/也可以为您提供帮助。