Miniblog

Miniblog is another variant of Asides. This application of Loop set aside all Posts of a specific category in a space separate from the rest of the Posts. This aggregation of Posts commonly resides in the sidebar.

  1. <?php get_header(); ?>
  2.  
  3. <div id="mainContent">
  4.  
  5. <?php $wp_query->set( 'cat', '-6' ); ?>
  6. <?php query_posts( '' ); ?>
  7.  
  8. <?php if( have_posts() ) : ?>
  9.  
  10. <?php while( have_posts() ) : the_post(); ?>
  11.  
  12. <div class="post" id="post-<?php the_ID(); ?>">
  13. <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
  14. <div class="meta"><?php the_category( ', ' ); ?> <?php the_author(); ?> <?php the_time('F jS, Y'); ?></div>
  15. <div class="entry">
  16. <?php the_excerpt(); ?>
  17. </div>
  18. </div>
  19.  
  20. <?php endwhile; ?>
  21.  
  22. <?php else : ?>
  23.  
  24. <div class="error">
  25. <h2>Not Found</h2>
  26. <p>Sorry, but you are looking for something that isn't here.</p>
  27. <?php include( TEMPLATEPATH . '/searchform.php' ); ?>
  28. </div>
  29.  
  30. <?php endif; ?>
  31.  
  32. </div>
  33.  
  34. <div id="sideContent">
  35.  
  36. <?php query_posts( 'category_name=Inline Function&showposts=10' ); ?>
  37.  
  38. <?php if( have_posts() ) : ?>
  39.  
  40. <h2>Inline Function</h2>
  41. <ul>
  42. <?php while( have_posts() ) : the_post(); ?>
  43.  
  44. <li id="post-<?php the_ID(); ?>"><?php echo wptexturize( $post->post_content ); ?></li>
  45.  
  46. <?php endwhile; ?>
  47. </ul>
  48.  
  49. <?php endif; ?>
  50.  
  51. </div>
  52.  
  53. <?php get_footer(); ?>

Just like Asides, Miniblog implementation requires the reservation of a category. However, unlike Asides, Miniblog requires two Loops to make it happen.

Line 5 of the code above set the variable $wp_query, which contains the information about the Posts to be displayed and is accessed heavily by the Loop, to have a query variable cat of -6 (minus six).

Fig. 1

Category id 6 has corresponding category name of Inline Function (Fig. 1). The minus sign indicates the exclusion of all Posts which belong to that category from being retrieved from the database. In turn, the Loop will never have Posts of that category id and only process the specified number of Posts of other category ids.

Line 6 retrieves the Posts from the database with the updated query variable set on line 5. Line 10-18 has the regular Loops which traverse the Posts recently retrieved.

On line 36, a query to retrieve another set of Posts is initiated again. All the query variables are reset and the category_name and showposts are set to Inline Function and 10. This query retrieves Posts of category Inline Function as many as 10. These Posts are excluded in the preceding query (line 5-6). Note that an identical query could constructed by passing the category id instead of its name. (Passing category id instead of category name reduces 1 query.)

  • <?php query_posts( 'cat=6&showposts=10' ); ?>

Line 42-46 proceeds with the second Loop to extract the Posts recently retrieved.

One caveat about miniblogging: When a Post is given multiple category–the Miniblog-assigned category and some other category, the Post will show up in both Miniblog section and in the other Loop section. Therefore, you may want to assign only one category to a Miniblog Post.

6 thoughts on “Miniblog

  1. Pingback: Rhymed Code » Feature

  2. Hmmm… I seem to get errors on line 5 and 36. The minus before the category ID seemed to cause a T STRING error, and I don’t remember what the other error said.

    I’m very sure I didn’t misspell anything, it was just copied and pasted and only the number and category name changed. Any idea what can be the problem?

    Thank you.

  3. I wonder if separate paginations are possible for each loop. At the moment i do have 2 loops with different categories displayed, and the main loop. But pagination for the main loop is screwed up …

Comments are closed.