- Custom post types
- Post metadata
- Custom taxonomies
Registering the Music Album Post Type
/* Plugin Name: Music Collection Post Types Plugin URI: http://example.com Description: Creates the music_album post type. Version: 0.1 */ /* Set up the post types. */ add_action( 'init', 'ssd_music_collection_register_post_types' ); /* Registers post types. */ function ssd_music_collection_register_post_types() { /* Set up the arguments for the 'music_album' post type. */ $album_args = array( 'public' => true, 'query_var' => 'music_album', 'rewrite' => array( 'slug' => 'music/albums', 'with_front' => false, ), 'supports' => array( 'title', 'thumbnail' ), 'labels' => array( 'name' => 'Albums', 'singular_name' => 'Album', 'add_new' => 'Add New Album', 'add_new_item' => 'Add New Album', 'edit_item' => 'Edit Album', 'new_item' => 'New Album', 'view_item' => 'View Album', 'search_items' => 'Search Albums', 'not_found' => 'No Albums Found', 'not_found_in_trash' => 'No Albums Found In Trash' ), ); /* Register the music album post type. */ register_post_type( 'music_album', $album_args ); }
Registering new taxonomies: Genre and Artist After you add the preceding code, you ’ ll be presented with two new submenu items under the Albums menu item in the admin, labeled Artists and Genres.
/* Set up the taxonomies. */ add_action( 'init', 'ssd_music_collection_register_taxonomies' ); /* Registers taxonomies. */ function ssd_music_collection_register_taxonomies() { /* Set up the artist taxonomy arguments. */ $artist_args = array( 'hierarchical' => false, 'query_var' => 'album_artist', 'show_tagcloud' => true, 'rewrite' => array( 'slug' => 'music/artists', 'with_front' => false ), 'labels' => array( 'name' => 'Artists', 'singular_name' => 'Artist', 'edit_item' => 'Edit Artist', 'update_item' => 'Update Artist', 'add_new_item' => 'Add New Artist', 'new_item_name' => 'New Artist Name', 'all_items' => 'All Artists', 'search_items' => 'Search Artists', 'popular_items' => 'Popular Artists', 'separate_items_with_commas' => 'Separate artists with commas', 'add_or_remove_items' => 'Add or remove artists', 'choose_from_most_used' => 'Choose from the most popular artists', ), ); /* Set up the genre taxonomy arguments. */ $genre_args = array( 'hierarchical' => true, 'query_var' => 'album_genre', 'show_tagcloud' => true, 'rewrite' => array( 'slug' => 'music/genres', 'with_front' => false ), 'labels' => array( 'name' => 'Genres', 'singular_name' => 'Genre', 'edit_item' => 'Edit Genre', 'update_item' => 'Update Genre', 'add_new_item' => 'Add New Genre', 'new_item_name' => 'New Genre Name', 'all_items' => 'All Genres', 'search_items' => 'Search Genres', 'parent_item' => 'Parent Genre', 'parent_item_colon' => 'Parent Genre:', ), ); /* Register the album artist taxonomy. */ register_taxonomy( 'album_artist', array( 'music_album' ), $artist_args ); /* Register the album genre taxonomy. */ register_taxonomy( 'album_genre', array( 'music_album' ), $genre_args ); }
Using a Taxonomy with Posts When using taxonomy with posts, you ’ ll generally be listing the taxonomy terms for the given post alongside some or all the content of the post. This would allow viewers to note there is a taxonomy for the post and allow them to fi nd related posts by a given taxonomy term.
add_action( 'init', 'ssd_music_album_register_shortcodes' ); function ssd_music_album_register_shortcodes() { /* Register the [music_albums] shortcode. */ add_shortcode( 'music_albums', 'ssd_music_albums_shortcode' ); } function ssd_music_albums_shortcode() { /* Query albums from the database. */ $loop = new WP_Query( array( 'post_type' => 'music_album', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1, ) ); /* Check if any albums were returned. */ if ( $loop->have_posts() ) { /* Open an unordered list. */ $output = '<ul class="music-collection">'; /* Loop through the albums (The Loop). */ while ( $loop->have_posts() ) { $loop->the_post(); /* Display the album title. */ $output .= the_title( '<li><a href="' . get_permalink() . '">', '</a></li>', false ); } /* Close the unordered list. */ $output .= '</ul>'; } /* If no albums were found. */ else { $output = '<p>No albums have been published.'; } /* Return the music albums list. */ return $output; }
A POST TYPE AND TAXONOMY PLUGIN It enables users to create new music albums and organize the albums by genre and artist.
- The fi rst step you take is creating a new fi le in your plugins directory named ssd-music-collection.php and adding your plugin header at the top of this fi le.
- The next step is to create the music_album post type, which was outlined in the Registering a Post Type section.
- The fi nal step of the process is creating the taxonomies for the music_album post type: album_artist and album_genre. How to register a taxonomy was covered in the Registering a Custom Taxonomy section.
- At this point, you have entire plugin with minimal code that creates and organizes a new type of content within WordPress. All that ’ s left is adding widgets , creating shortcodes, ...

One comment
Leave a Comment