Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

foreach loop for Custom Post Types Examples #245

Closed
wants to merge 1 commit into from

2 participants

@dhigginbotham

using this with my bones and thought it may be something others would want in... very basic but works well for me. just add to the $post_types and, magic.

@eddiemachado

This is great. I'll mark it for future use. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 7, 2012
  1. made a foreach loop out of the custom post types

    David Higginbotham authored
This page is out of date. Refresh to see the latest.
Showing with 50 additions and 44 deletions.
  1. +50 −44 library/custom-post-type.php
View
94 library/custom-post-type.php
@@ -16,52 +16,58 @@
// let's create the function for the custom type
-function custom_post_example() {
- // creating (registering) the custom type
- register_post_type( 'custom_type', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
- // let's now add all the options for this post type
- array('labels' => array(
- 'name' => __('Custom Types', 'bonestheme'), /* This is the Title of the Group */
- 'singular_name' => __('Custom Post', 'bonestheme'), /* This is the individual type */
- 'all_items' => __('All Custom Posts', 'bonestheme'), /* the all items menu item */
- 'add_new' => __('Add New', 'bonestheme'), /* The add new menu item */
- 'add_new_item' => __('Add New Custom Type', 'bonestheme'), /* Add New Display Title */
- 'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
- 'edit_item' => __('Edit Post Types', 'bonestheme'), /* Edit Display Title */
- 'new_item' => __('New Post Type', 'bonestheme'), /* New Display Title */
- 'view_item' => __('View Post Type', 'bonestheme'), /* View Display Title */
- 'search_items' => __('Search Post Type', 'bonestheme'), /* Search Custom Type Title */
- 'not_found' => __('Nothing found in the Database.', 'bonestheme'), /* This displays if there are no entries yet */
- 'not_found_in_trash' => __('Nothing found in Trash', 'bonestheme'), /* This displays if there is nothing in the trash */
- 'parent_item_colon' => ''
- ), /* end of arrays */
- 'description' => __( 'This is the example custom post type', 'bonestheme' ), /* Custom Type Description */
- 'public' => true,
- 'publicly_queryable' => true,
- 'exclude_from_search' => false,
- 'show_ui' => true,
- 'query_var' => true,
- 'menu_position' => 8, /* this is what order you want it to appear in on the left hand side menu */
- 'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
- 'rewrite' => array( 'slug' => 'custom_type', 'with_front' => false ), /* you can specify its url slug */
- 'has_archive' => 'custom_type', /* you can rename the slug here */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- /* the next one is important, it tells what's enabled in the post editor */
- 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
- ) /* end of options */
- ); /* end of register post type */
+function custom_posts_loop() {
- /* this adds your post categories to your custom post type */
- register_taxonomy_for_object_type('category', 'custom_type');
- /* this adds your post tags to your custom post type */
- register_taxonomy_for_object_type('post_tag', 'custom_type');
+ $post_types = array('customType','anotherCustomType'); //just add more types to this array, ie this example
-}
+ foreach ($post_types as $slug) {
+ // creating (registering) the custom type
+ register_post_type( $slug, /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
+ // let's now add all the options for this post type
+ array('labels' => array(
+ 'name' => __(ucfirst($slug), 'bonestheme'), /* This is the Title of the Group */
+ 'singular_name' => __(ucfirst($slug), 'bonestheme'), /* This is the individual type */
+ 'all_items' => __('All ' . ucfirst($slug), 'bonestheme'), /* the all items menu item */
+ 'add_new' => __('Add New', 'bonestheme'), /* The add new menu item */
+ 'add_new_item' => __('Add New ' . ucfirst($slug), 'bonestheme'), /* Add New Display Title */
+ 'edit' => __( 'Edit', 'bonestheme' ), /* Edit Dialog */
+ 'edit_item' => __('Edit Post ' . ucfirst($slug), 'bonestheme'), /* Edit Display Title */
+ 'new_item' => __('New', 'bonestheme'), /* New Display Title */
+ 'view_item' => __('View', 'bonestheme'), /* View Display Title */
+ 'search_items' => __('Search ' . ucfirst($slug), 'bonestheme'), /* Search Custom Type Title */
+ 'not_found' => __('Nothing found in the Database.', 'bonestheme'), /* This displays if there are no entries yet */
+ 'not_found_in_trash' => __('Nothing found in Trash', 'bonestheme'), /* This displays if there is nothing in the trash */
+ 'parent_item_colon' => ''
+ ), /* end of arrays */
+ 'description' => __( 'Section for ' . ucfirst($slug), 'bonestheme' ), /* Custom Type Description */
+ 'public' => true,
+ 'publicly_queryable' => true,
+ 'exclude_from_search' => false,
+ 'show_ui' => true,
+ 'query_var' => true,
+ 'menu_position' => 8, /* this is what order you want it to appear in on the left hand side menu */
+ 'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
+ 'rewrite' => array( 'slug' => $slug, 'with_front' => false ), /* you can specify its url slug */
+ 'has_archive' => $slug, /* you can rename the slug here */
+ 'capability_type' => 'post',
+ 'hierarchical' => false,
+ /* the next one is important, it tells what's enabled in the post editor */
+ 'supports' => array( 'title', 'editor', 'author', 'thumbnail')
+ ) /* end of options */
+ ); /* end of register post type */
+
+ /* this adds your post categories to your custom post type */
+ register_taxonomy_for_object_type('category', $slug);
+ /* this adds your post tags to your custom post type */
+ register_taxonomy_for_object_type('post_tag', $slug);
+
+ }
+
+ }
// adding the function to the Wordpress init
- add_action( 'init', 'custom_post_example');
-
+ add_action( 'init', 'custom_posts_loop');
+
/*
for more information on taxonomies, go here:
http://codex.wordpress.org/Function_Reference/register_taxonomy
@@ -69,7 +75,7 @@ function custom_post_example() {
// now let's add custom categories (these act like categories)
register_taxonomy( 'custom_cat',
- array('custom_type'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
+ array('customType'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
array('hierarchical' => true, /* if this is true, it acts like categories */
'labels' => array(
'name' => __( 'Custom Categories', 'bonestheme' ), /* name of the custom taxonomy */
@@ -91,7 +97,7 @@ function custom_post_example() {
// now let's add custom tags (these act like categories)
register_taxonomy( 'custom_tag',
- array('custom_type'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
+ array('customType'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
array('hierarchical' => false, /* if this is false, it acts like tags */
'labels' => array(
'name' => __( 'Custom Tags', 'bonestheme' ), /* name of the custom taxonomy */
Something went wrong with that request. Please try again.