Monthly Archives: April 2010

Anchor Links/Jump Links

This little snippet of code will generate ‘jump links’ to each ‘Next Post’ on a page.

<?php if (!$jumpcnt) $jumpcnt = 0;
   echo "<a name='jump$jumpcnt'></a>";
   ++$jumpcnt;
   echo "<a href='#jump$jumpcnt'>Next Post</a>";?>

Show X Posts For Y Categories

This code will show X posts per category for Y number of categories.

The Latest Post From Each Category Plugin might work, but has not been updated in a while.

<?php
$num_cats_to_show = 10;
$num_posts_per_cat = 1;
$count = 0;
$taxonomy = 'category';//  e.g. post_tag, category
$param_type = 'category__in'; //  e.g. tag__in, category__in
$term_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => $num_posts_per_cat,
      'caller_get_posts'=> 1
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      $count ++;
      if ($count <= $num_cats_to_show) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
         <?php
        endwhile;
      }
    }
  }
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

To select only specific categories, add the ‘include’ parameter to the $term_args array, like this:

$term_args=array(
  'orderby' => 'name',
  'order' => 'ASC',
  'include' => array(23,51,12),  // A list of IDs to include
);

Short First Page

The code below will show a smaller number of posts on the first page.  WILL NOT WORK FOR LONGER FIRST PAGE!

Set the ‘Blog pages show at most’ in Admin->Settings->Reading to the number to show on all pages except the first.

<?php $max_first_page = 3;  // Show this many posts on front page
      $args = $wp_query->query;
      $args['caller_get_posts'] = 1; // Using stickies messes up the count
      $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $posts_per_page = get_query_var('posts_per_page');
      $posts_to_skip = $posts_per_page - $max_first_page;
      if ($page == 1) {
         $max_posts = $max_first_page;
      } else {
         $max_posts = $posts_per_page;
         $args['offset'] = (($page - 1) * $max_posts)- $posts_to_skip;
      }
      query_posts($args);
      if ($wp_query->max_num_pages < ceil(($wp_query->found_posts + $posts_to_skip)/$posts_per_page))
        ++$wp_query->max_num_pages;
      $counter = 0;
      ?>
   <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
         <?php if (++$counter > $max_posts) continue; ?>
             // Rest of the loop