Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save encoderit-arman/f76243d2396e8f39f2800454ac7e92f8 to your computer and use it in GitHub Desktop.
Save encoderit-arman/f76243d2396e8f39f2800454ac7e92f8 to your computer and use it in GitHub Desktop.
function create_custom_post_types()
{
$post_type = [
'wps-service' => [
'name' => 'Services',
'title' => true,
'slug' => 'wps-service',
'is_category' => true,
'taxonomy' => 'wps-service-taxonomy',
'icon' => 'dashicons-networking',
'editor' => true,
'thumbnail' => true,
'show_in_menu' => true,
],
];
$i = 5;
foreach ($post_type as $k => $v) {
if ($v['editor']) {
$editor = 'editor';
} else {
$editor = '';
}
if ($v['thumbnail']) {
$thumbnail = 'thumbnail';
} else {
$thumbnail = '';
}
if ($v['title']) {
$title = 'title';
} else {
$title = '';
}
if ($v['show_in_menu']) {
$show_in_menu = true;
} else {
$show_in_menu = false;
}
register_post_type(
$k,
array(
'labels' => array(
'name' => __($v['name']),
'singular_name' => __($v['name']),
'add_new' => __('Add New'),
'add_new_item' => __('Add New ' . $v['name']),
'edit_item' => __('Edit ' . $v['name']),
'new_item' => __('New ' . $v['name']),
'view_item' => __('View ' . $v['name']),
'not_found' => __('Sorry, we couldn\'t find the ' . $v['name'] . ' Menu you are looking for.')
),
'public' => true,
'show_in_menu' => $show_in_menu,
'publicly_queryable' => true,
'exclude_from_search' => true,
'menu_position' => $i,
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => $v['icon'],
'taxonomies' => array($v['taxonomy']),
'capability_type' => 'page',
'rewrite' => array('slug' => $v['name']),
'supports' => [$title, $editor, $thumbnail]
)
);
if ($v['is_category']) {
$labels = array(
'name' => _x($v['name'], 'taxonomy general name'),
'singular_name' => _x(' Category', 'taxonomy singular name'),
'search_items' => __('Search '),
'all_items' => __('All '),
'parent_item' => __('Parent Category'),
'parent_item_colon' => __('Parent Category:'),
'edit_item' => __('Edit Category'),
'update_item' => __('Update Category'),
'add_new_item' => __('Add New Category'),
'new_item_name' => __('New Category'),
'menu_name' => __(' Category'),
'rewrite' => array(
'slug' => $v['taxonomy'],
'with_front' => false
)
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy($v['taxonomy'], array($k), $args);
$i++;
}
}
}
add_action('init','create_custom_post_types');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment