Support » Themes and Templates » Creating an Archive by Category

  • Hi all ..

    I’ve been trying to create an archive to display lists of categories.
    I need to display latest post in each categories.

    I’ve tried to use get_categories and get_posts to not directly access the database using wpdb class.

    Unfortunately, get_posts retrieved the same post list on each categories, so I need to find another way. I’m able to do that and you can see the result on my blog:

    It’s all alright but I need your opinion on my script. Am I doing good or is there any better solution for this.

    Here’s the scirpt I use on my template

    $categories = get_categories();
    foreach( $categories as $cat ) {
    	// Get the category ID
    	$cat_ID	= get_cat_ID( $cat->cat_name );
    	$archive .= '<div class="post entry-summary entry">' . "\n";
    	$archive .= '<h3><a href="' . get_category_link( $cat_ID ) . '">' . $cat->cat_name . '</a> (' . $cat->category_count . ')</h3>' . "\n";
    	$archive .= '<p>' . $cat->category_description . '</p>' . "\n";
    	// Get the term id for current category
    	$the_terms = $wpdb->get_results("SELECT wp_term_taxonomy.term_taxonomy_id as the_term_id FROM $wpdb->term_relationships JOIN $wpdb->term_taxonomy ON $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_id WHERE $wpdb->term_taxonomy.term_id = $cat_ID AND $wpdb->term_taxonomy.taxonomy = 'category' GROUP BY $wpdb->term_relationships.term_taxonomy_id");
    	$the_term_ID = $the_terms[0]->the_term_id;
    	// Get the posts for current term id on a descending order and limit the result to 3
    	$the_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts JOIN $wpdb->term_relationships ON $wpdb->posts.ID = $wpdb->term_relationships.object_id WHERE $wpdb->posts.post_type = 'post' AND $wpdb->posts.post_status = 'publish' AND $wpdb->term_relationships.term_taxonomy_id = $the_term_ID GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 3");
    	$archive .= '<ul class="archives">' . "\n";
    	foreach ( $the_posts as $the_post ) {
    		$archive .= '<li class="archive-post-title">' . mysql2date( get_option( 'date_format' ), $the_post->post_date ) . ' : <a href="' . get_permalink( $the_post->ID ) . '">' . $the_post->post_title . '</a></li>' . "\n";
    	$archive .= '</ul>' . "\n";
    	$archive .= '</div>' . "\n";
    	// Output the list
    	print $archive.
Viewing 1 replies (of 1 total)
  • Don’t be confused by what an archive is–it is just a generated display of your posts at that moment. You don’t do anything to create the archive, that’s an automatic thing WordPress does for you. Usually, archives are date, category, tag, or author, based.

    Access to archives is typically presented via links in a sidebar under an Archive (date based), Category, or Tag Cloud, headings. Widgets, or Template Tags such as wp_get_archives(), wp_list_categories(), wp_tag_cloud(), and wp_list_authors(), are the constructs used to present links to users to visit your various archives. The process of placing code in your Theme’s Templates is explained in Stepping Into Templates and Stepping Into Template Tags.

    Once a user clicks on a Category link in the sidebar, the display of those posts can be controlled by a Category Template. Other Templates, such as Author Templates, and Tag Templates, are available if you set them up. These Templates can be coded via Template Tags such as the_title(), the_content(), or the_excerpt(), to display just a post title, the full content of the post, or just an excerpt of the post.

    Also, it is important to understand the Template Hierarchy, as that is how WordPress determines what Template to use to render the posts for reading by your readers.

    If a user visits a Category archive, then clicks on a given post title in that Category archive, the display of that single post is again presented by another Template, and again, the Template Hierarchy determines what Template displays that single post. Finally, that single post Template can be coded to display just the title, the full post content, or an excerpt.

Viewing 1 replies (of 1 total)
  • The topic ‘Creating an Archive by Category’ is closed to new replies.