Show Latest Post From Each of Specified Categories
The Code below illustrates how to select the latest Post from each of a list of categories.
Continue reading
Post Titles By Category
This code will list post titles, with links to the full posts, by category name.
It does not provide pagination, so the page could get quite long. On a large site, the number of queries could cause timeouts or server overload.
It is not complete by itself as it needs to be integrated with your theme by putting in the appropriate get_header(), divs, get_sidebar(), etc.
Continue reading
Page Links To Plugin, aka Categories on a Page
How do I show a Category on a Page? Or, for that matter, how do I link to anything I want? This question comes up regularly on the Support forums.
The question really breaks down into two parts:
- How do I show categories on a page?
- How do I keep from duplicating the posts on the Home/Blog page?
There are several ways to do this, depending on your theme and the version of WordPress you are using. Continue reading
Add a Tag to each Post in a Category
A WP user had a large number of posts in a certain category. They wanted to add a tag to each post in that category. Here is the procedure to do that with SQL commands. 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\">« PREV </span>";
}else{
$pageprev = $page - 1;
$pagelinks .= "<a class=\"pageprevlink\" href=\"" . $currpage .
"&page=" . $pageprev . "\">« 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 .= " ... ";
} else {
$pagelinks .= " ";
}
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 .= " ... ";
$pagelinks .= "<a class=\"pagenumlink\" " .
"href=\"" . $currpage . "&page=" . $numofpages .
"\"> [" . $numofpages . "] </a>";
} else {
$pagelinks .= " ";
}
if(($numrows - ($limit * $page)) > 0){
$pagenext = $page + 1;
$pagelinks .= "<a class=\"pagenextlink\" href=\"" . $currpage .
"&page=" . $pagenext . "\"> NEXT »</a>";
} else {
$pagelinks .= "<span class=\"pagenextdead\"> NEXT »</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
Custom Taxonomies
Custom taxonomies are a great way to tailor WordPress to your exact needs. Unfortunately, most people never use them because they seem too complicated. The articles below will help to clear up the fog and let you use these tools.
Justin Tadlock on Custom Taxonomies
CODEX: add_meta_box to add a meta box for hierarchical custom taxonomies
CODEX: Adding Admin Menus for adding a menu item for hierarchical custom taxonomies
Category Pagination Fix
Apparently there was a problem with category pagination and pretty permalinks in version 2.7 (and others?). This plugin claims to be a fix:
http://www.htmlremix.com/projects/category-pagination-wordpress-plugin
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.
<?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 );