code

Show posts one day at a time

The code below retrieves an array of dates which have posts and shows the posts for one day at a time. Continue reading

Automatically Add Categories to Posts Based on Tags

The code below will automatically add categories to a post when it is published or updated.  This operates on one tag at a time and adds a category each time it finds a tag in the post that matches a tag in the $tag_categories array. So, if a post is in ‘info’, it will add category ‘information’, etc. Since it loops through all the tags for a post, it might add several categories. Continue reading

List categories by letter

The code below is for a template for the WP 2.9 default theme that lists categories by the first letter of the category name.

An updated version of this code, with the capability to list terms of any taxonomy is shown in this post.

<?php
/**
 * @package WordPress
 * @subpackage Default_Theme
 */
/*
Template Name: listcatsbyletter
*/
?>
<?php /*
 Author: Mac McDonald
 Contact at BluegrassMiataClub.com using About->Contact Us form.

 This program creates a list of categories as links to the archive for
 each category.

 Set $hide_uncategorized = false; to show the uncategorized category.
     $hide_description = true; to hide the description of each category.
     $limit = the number of categories (not including Letters) per page.
     $range = number of page links in the nav bar. (5 is a good number).
*/?>

<?php get_header(); ?>

<?php
$hide_uncategorized = true;
$hide_description = false;
$limit = 25;  // Show this many entries per page
$range = 5;  // Show this many nav bar page links (other than first & last).
$args = array('orderby' => 'name', 'order' => 'ASC');
if ($hide_uncategorized) $args['exclude'] = '1';
$categories = get_categories($args);
if ($categories) :
   // For some unknown reason, $categories has no [0] entry at times, so renumber.
   $categories = array_merge($categories,array());
   if (isset($_GET['page'])) {
      $page = $_GET['page'];
   } else {
      $page = 1;
   }
   $start = ($page - 1) * $limit;
   $currletter = '';
   echo '<ul class="category_list">';
   for ($i=$start;$i<($start + $limit);++$i) {
      if ($i < sizeof($categories)) {
         $cat =  $categories[$i];
         $catletter = strtoupper(substr($cat->name,0,1));
         if ($currletter != $catletter) {
            if ($currletter != '') echo '</ul>';
            $currletter = $catletter;
            echo "<h2> - $catletter - </h2>";
            echo '<ul>';
         }
         echo '<li class="category_entry">Category: <a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a> </p> ';
         if (!$hide_description) echo '<p>Description: '. $cat->description . '</p>';
         echo '</li>';
      }
   }
   echo '<br /><br />';
   echo _cat_paginate(sizeof($categories),$limit,$range);
   echo '</ul>';
else:
   echo '<h2></h2>';
   echo 'There are no Categories to list';
endif;?>

<?php get_footer(); ?>

<?php function _cat_paginate($numrows,$limit=10,$range=7) {

   $pagelinks = "<div class=\"pagelinks\">";
   $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
   if ($numrows > $limit) {
      if(isset($_GET['page'])){
         $page = $_GET['page'];
      } else {
         $page = 1;
      }
      $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
      $currpage = str_replace("&page=".$page,"",$currpage);
      if($page == 1){
         $pagelinks .= "<span class=\"pageprevdead\">&laquo PREV </span>";
      }else{
         $pageprev = $page - 1;
         $pagelinks .= "<a class=\"pageprevlink\" href=\"" . $currpage .
               "&page=" . $pageprev . "\">&laquo PREV </a>";
      }
      $numofpages = ceil($numrows / $limit);
      if ($range == "" or $range == 0) $range = 7;
      $lrange = max(1,$page-(($range-1)/2));
      $rrange = min($numofpages,$page+(($range-1)/2));
      if (($rrange - $lrange) < ($range - 1)) {
         if ($lrange == 1) {
            $rrange = min($lrange + ($range-1), $numofpages);
         } else {
            $lrange = max($rrange - ($range-1), 0);
         }
      }
      if ($lrange > 1) {
         $pagelinks .= "<a class=\"pagenumlink\" " .
            "href=\"" . $currpage . "&page=" . 1 .
            "\"> [1] </a>";
         if ($lrange > 2) $pagelinks .= "&nbsp;...&nbsp;";
      } else {
         $pagelinks .= "&nbsp;&nbsp;";
      }
      for($i = 1; $i <= $numofpages; $i++){
         if ($i == $page) {
            $pagelinks .= "<span class=\"pagenumon\"> [$i] </span>";
         } else {
            if ($lrange <= $i and $i <= $rrange) {
               $pagelinks .= "<a class=\"pagenumlink\" " .
                        "href=\"" . $currpage . "&page=" . $i .
                        "\"> [" . $i . "] </a>";
            }
         }
      }
      if ($rrange < $numofpages) {
         if ($rrange < $numofpages - 1) $pagelinks .= "&nbsp;...&nbsp;";
            $pagelinks .= "<a class=\"pagenumlink\" " .
               "href=\"" . $currpage . "&page=" . $numofpages .
               "\"> [" . $numofpages . "] </a>";
      } else {
         $pagelinks .= "&nbsp;&nbsp;";
      }
      if(($numrows - ($limit * $page)) > 0){
         $pagenext = $page + 1;
         $pagelinks .= "<a class=\"pagenextlink\" href=\"" . $currpage .
                    "&page=" . $pagenext . "\"> NEXT &raquo;</a>";
      } else {
         $pagelinks .= "<span class=\"pagenextdead\"> NEXT &raquo;</span>";
      }

   }
$pagelinks .= "</div>";
return $pagelinks;
}
?>

Stay in Category

The text of this article is also in a pastebin – http://pastebin.com/4fLC9FLy

In many themes, the previous/next_post_link in single.php does not stay in category. If you view a category archive, then select a single post, the prev/next link may not show a post in the same category.

If you set the ‘in_same_cat’ parameter to the previous/next_post_link calls in single.php to ‘true’, single.php will stay in category provided the posts belong only to one category. If posts belong to more than one category, the prev/next links may point to posts in one of the categories other than the one from the category archive. Changing ‘in_same_cat’ will also affect how a blog works, because you normally don’t want to stay in category when coming from a blog post list.
Continue reading

Filters to Modify a Query

UPDATE: See the excellent variation by keesiemeijer here: https://gist.github.com/4643765

Many times it is necessary to use a filter to modify a query.  For example, you may want to include a column that WP does not normally include in a query.  Or, you may want to do some complex selection of posts based on criteria that query_posts does not supply.  Continue reading

First and Last Post on a Page

If you want to detect the first and last post on a page (not the entire query), you can use the code below:

if (have_posts()) : while (have_posts()) : the_post();
  $c++;
  if( $c == 1 ) {
    $style='first';
  } elseif ( $c == $wp_query->post_count ) ) {
    $style='last';?>
  }
  // Rest of the code for the Loop

Select meta_value between limits

This code was written for a user to select posts based on the value of a Custom Field between min and max limits. Continue reading

Pagination of Array / Custom Query


NOTE:

As of WP 3.3.1, I have had problems using this approach with pretty permalinks. Please use the paginate_links() technique instead.


The mam_paginate() function shown here can be used to paginate a Custom Query or any array. I use it instead of paginate_links() when I want more control over the output. It can also be used outside of WordPress with only slight modification. Continue reading

Add [raw] shortcode to display code

NOTE: This code appears to fail in WP 3.0.  Use the SyntaxHighlighter Evolved plugin instead. Continue reading

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>";?>