WordPress - Custom Post Types och Custom Taxonomies
Jag har läst igenom lite bloggar och på Codex:en om hur Custom Post Types och Custom Taxonomies fungerar.
Just nu har jag skapat två Custom Post Types, "News" och "Articles". Här är koden:
<?php
// Creates the post-type 'Articles' and 'News'
function create_my_post_types() {
register_post_type( 'Articles',
array(
'labels' => array(
'name' => __('Articles', 'beinnovate'),
'singular_name' => __('Article', 'beinnovate'),
'add_new' => __('Create new', 'beinnovate'),
'add_new_item' => __('Create new Article', 'beinnovate'),
'edit' => __('Edit', 'beinnovate'),
'edit_item' => __('Edit Article', 'beinnovate'),
'new_item' => __('New Article', 'beinnovate'),
'view' => __('View', 'beinnovate'),
'view_item' => __('View Article', 'beinnovate'),
'search_items' => __('Search articles', 'beinnovate'),
'not_found' => __('No Article found', 'beinnovate'),
'not_found_in_trash' => __('No Articles found in trash', 'beinnovate'),
'parent' => __('Parent Article', 'beinnovate'),
),
'public' => true,
'show_ui' => true,
'exclude_from_search' => false,
'menu_position' => 20,
'capability_type' => 'page',
'hierarchical' => true,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),
'rewrite' => array('slug' =>'articles', 'with_front' => false),
'can_export' => true,
'show_in_nav_menus' => true,
)
);
register_post_type( 'News',
array(
'labels' => array(
'name' => __('News', 'beinnovate'),
'singular_name' => __('News', 'beinnovate'),
'add_new' => __('Add News', 'beinnovate'),
'add_new_item' => __('Create News', 'beinnovate'),
'edit' => __('Edit', 'beinnovate'),
'edit_item' => __('Edit News', 'beinnovate'),
'new_item' => __('New Novelty', 'beinnovate'),
'view' => __('View', 'beinnovate'),
'view_item' => __('View News', 'beinnovate'),
'search_items' => __('Search News', 'beinnovate'),
'not_found' => __('No News found', 'beinnovate'),
'not_found_in_trash' => __('No News found in trash', 'beinnovate'),
'parent' => __('Parent News', 'beinnovate'),
),
'public' => true,
'show_ui' => true,
'exclude_from_search' => false,
'menu_position' => 5,
'capability_type' => 'post',
'hierarchical' => true,
'supports' => array('title', 'editor'),
'rewrite' => array('slug' =>'news', 'with_front' => true),
'can_export' => true,
'show_in_nav_menus' => true,
)
);
}
// Register taxonomies for the Articles
register_taxonomy_for_object_type('post_tag', 'Articles');
Det fungerar, och jag har skapat två Custom Taxonomies, "Tags" och "Category", som är knutna till min Custom Post Type Articles också med denna koden:
//Register taxonomies for the custom post type "Articles"
register_taxonomy( 'tags', 'Articles',
array(
'labels' => array(
'label' => __('Tags', 'beinnovate'),
'name' => __('Tags', 'beinnovate'),
'singular_name' => __('Tag', 'beinnovate'),
'search_items' => __('Search Tags', 'beinnovate'),
'popular_items' => __('Popular Tags', 'beinnovate'),
'all_items' => __('All Tags', 'beinnovate'),
'edit_item' => __('Edit Tag', 'beinnovate'),
'update_item' => __('Update Tag', 'beinnovate'),
'add_new_item' => __('Add New Tag', 'beinnovate'),
'new_item_name' => __('New Tag Name', 'beinnovate'),
'separate_items_with_comma' => __('Separate tags with commas', 'beinnovate'),
'add_or_remove_items' => __('Add or remove tags', 'beinnovate'),
'choose_from_most_used' => __('Choose from most used', 'beinnovate'),
'hierarchical' => false
)
)
);
register_taxonomy( 'Category', 'Articles',
array(
'labels' => array(
'label' => __('Category', 'beinnovate'),
'name' => __('Categories', 'beinnovate'),
'singular_name' => __('Category', 'beinnovate'),
'search_items' => __('Search Categories', 'beinnovate'),
'all_items' => __('All Categories', 'beinnovate'),
'parent_item' => __('Parent Category', 'beinnovate'),
'edit_item' => __('Edit Category', 'beinnovate'),
'update_item' => __('Update Category', 'beinnovate'),
'add_new_item' => __('Add New Category', 'beinnovate'),
'separate_items_with_comma' => __('Separate categories with commas', 'beinnovate'),
'new_item_name' => __('New Cateogry Name', 'beinnovate'),
'hierarchical' => true
)
)
);
add_action( 'init', 'create_my_post_types' );
Jag tror att det är rätt som jag har har gjort det? De finns i alla fall att använda när jag är inne på Articles.
Nu är min fråga: Hur kan jag visa information från mina Taxonomies när jag är inne på olika "Articles"? Så här ser min loop ut nu:
<div id="content" class="grid_14 prefix_1 suffix_1">
<!-- here starts the loop -->
<?php if(have_posts()) : ?> <!-- checks to see if there is any post -->
<?php while(have_posts()) : the_post(); ?> <!-- if there is, execute the post -->
<div class="article" id="article-<?php the_id(); ?>" title="<?php the_title(); ?>"> <!-- each post start and gives each post a unique ID and a title -->
<h1><?php the_title(); ?></h1> <!-- this shows the title of the post and makes it a link -->
<div class="entry"> <!-- the div for the entry in the post -->
<?php the_content(); ?> <!-- shows the content of the post -->
</div> <!-- end entry -->
<div id="article-meta-data"> <!-- article information -->
<ul>
<li><?php _e('Written by ', 'beinnovate'); the_author(); ?></li>
<li><?php _e('Posted in: ', 'beinnovate'); ?> </li>
<li><?php _e('Tagged with: ', 'beinnovate'); ?><?php the_tags(); ?></li>
<li><?php _e('Published at ', 'beinnovate'); ?><?php the_date(); ?></li>
</ul>
</div> <!-- end article information -->
</div> <!-- end post -->
<?php endwhile; ?> <!-- close while -->
<?php endif; ?> <!-- close if -->
<!-- end the loop -->
</div> <!-- end content -->
Men det fungerar inre att göra som jag har gjort det med att använda the_tags(); eller the_date();
Hur ska jag göra för att kunna hämta informationen från mina taxonomies när jag är inne på min artikel?
//Andreas Olsson