Here is the second part of a tutorial on how to develop WordPress templates, it explains the loop, custom fields and the categories.
To access other parts of the tutorial see the WordPress Template Tutorial post.
There are several methods to get the posts data, by default they are available within “the loop”, which is the code inside a template file that parses the default data given by WordPress. It has this form:
if (have_posts()) : while (have_posts()) : the_post(); // Inside The Loop is here endwhile; else : // Not Found endif;
Within “the loop” you can access the posts data, the posts are automatically filtered depending by the template file you are working on.
All the return data is accessible inside “the loop” using particular functions called template tags, here are some:
the_ID(); the_author(); get_the_author(); the_author_link(); get_the_author_link(); the_title(); get_the_title($post->ID); the_permalink(); get_permalink($post->ID); the_time('F jS, Y'); the_content(); the_excerpt(); the_tags('Tags: ', ', ', '<br />'); the_category(', '); get_the_category(); comments_popup_link('No Comments', '1 Comment', '% Comments');
Or inside a $post object:
$post->post_author; $post->post_date; $post->post_date_gmt; $post->post_content; $post->post_title; $post->post_excerpt; $post->post_name; $post->post_modified; $post->post_modified_gmt; $post->post_parent; $post->guid; $post->menu_order; $post->post_type; $post->comment_count;
Still inside the loop, you can retrieve the custom fields data of a post with this code:
// Dump out all custom fields as a list the_meta(); // Display value of one specific custom field $meta = get_post_meta($post->ID, 'CustomFieldName', true); if ($meta) {} // Display multiple values of same custom field name $metas = get_post_meta($post->ID, 'CustomFieldName', false); sort($metas); foreach($metas as $meta) { echo '<li>'.$meta.'</li>'; }
But if we are using Magic Fields the custom fields data will be retrieved with this code (more informations here):
get('field_name');
There are some core functions to work with the categories.
The first is get_the_category which is used to parse the categories are in the posts in the current page:
// get_the_category $category=get_the_category($post->ID); // Optional argument the post ID echo $category[0]->cat_ID; echo $category[0]->cat_name; // You can also loop trought the results foreach((get_the_category()) as $category) { echo $category->cat_ID; echo $category->cat_name; echo $category->category_nicename; // slug echo $category->category_description; echo $category->category_parent; // 0 = no parents echo $category->category_count; }
The second function is get_categories which serves when you have to do a more complex category query.
// get_categories $args = array( 'type' => 'post', 'child_of' => 0, // Display all categories that are descendants of category ID 'parent' => 0, // Display all categories that are 1 depth descendants of category ID 'orderby' => 'name', // id, name, slug, count, group 'order' => 'ASC', // asc or desc 'hide_empty' => 1, // 1 or 0 'hierarchical' => 1, 'exclude' => '', // List of categories by ID, in ascending order 'include' => '', // List of categories by ID, in ascending order 'number' => 10, // The number of categories to return 'taxonomy' => 'category' // Taxonomy to return. category or taxonomy 'pad_counts' => false ); $categories = get_categories( $args ); foreach($categories as $category) { echo $category->cat_ID; echo $category->cat_name; echo $category->category_nicename; // slug echo $category->category_description; echo $category->category_parent; // 0 = no parents echo $category->category_count; }
The third function is wp_list_categories which displays a list of ALL categories as links, very useful when you need to build the menu of the website.
$args = array( 'show_option_all' => '', 'orderby' => 'id', // id, name, slug, count, group 'order' => 'ASC', // asc or desc 'show_last_update' => 0, // 1 or 0 'style' => 'list', // list or none 'show_count' => 0, // 1 or 0 'hide_empty' => 1, // 1 or 0 'use_desc_for_title' => 0, // 1 or 0 'child_of' => 0, // Display all categories that are descendants of category ID 'exclude' => '', // List of categories by ID, in ascending order 'exclude_tree' => '', // List of categories by ID, in ascending order 'include' => '', // List of categories by ID, in ascending order 'hierarchical' => 1, // Display sub-categories as inner list items. 1 or 0 'title_li' => __( '' ), // Set the title and style of the outer list item 'number' => NULL, // The number of categories to return 'echo' => 0, // Show the result (1) or keep it in a variable (0) 'depth' => 1, // 0: All Categories and child Categories, 1: Show only top level Categories, n 'current_category' => 0, // Allows you to force the "current-cat" 'pad_counts' => 0, 'taxonomy' => 'category' // Taxonomy to return. category or taxonomy ); $categories = wp_list_categories( $args ); $categories = str_replace('View all posts filed under ', '', $categories); // Useful if you have to change something echo $categories;
End of the second part of the WordPress Template Tutorial.
Next part of the tutorial will cover custom queries, the WP_Query, the get_posts and the query_posts.
Comments







