solostream

Category: Code Snippets

Display recent posts in WordPress sidebar

| November 11, 2007 | 65 Comments
Here's a bit of code to put in your sidebar to display the most recent posts on your blog: <?php $myposts = get_posts('numberposts=10&offset=1'); foreach($myposts as $post) :?> <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li> <?php endforeach; ?> You can set the number of recent posts to appear on the first line of code where it says numberposts=10 - change the 10 ... View Post

Recent Comments code snippet

| November 9, 2007 | 37 Comments
This code is from the Blog Oh Blog theme, and displays recent comments in the sidebar without the need for a plugin: <?php global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password ... View Post

Display posts from specific categories on a Page

| November 8, 2007 | 95 Comments
You may want to display posts from a specific category on the homepage or other pages of your site. For example, you might want to have a box that displays the latest three posts from your News category, or have links to your latest podcasts.... View Post

Creating a feature post or sticky post in a WordPress blog

| October 29, 2007 | 3 Comments
To get a post from a specific category to appear at the top of the page above the rest of the posts, and to only change when a new post from that particular category is added, use the following code: <?php /*?><?php $my_query = new WP_Query('category_name=featured&showposts=1'); while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;?> <div class="featureItem"> <h1>Feature: <a href="<?php the_permalink() ... View Post

Creating different Page templates

| October 29, 2007 | 0 Comments
To create a new Page template, add the following code to the top of the page: <?php /* Template Name: Home */ ?> The Template Name can be changed, i.e. Home can be About Us, or anything else. When you create a Page in WordPress, on the right-hand side there is a drop down box for selecting a template. The ... View Post

Display a selected number of posts from one category in the sidebar

| October 29, 2007 | 30 Comments
<?php$postlist = get_posts('category=6&numberposts=5'); foreach ($postlist as $post) :?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php endforeach; ?> Use this in the sidebar to display a defined number of posts from a specified category. Where it says "category=6" replace the 6 with the relevant category ID number, and where it says "numberposts=5" replace the 5 with the number of ... View Post

Code snippets on WordPressGarage

| October 29, 2007 | 0 Comments
I am going to start posting code snippets on WordPressGarage that are used in WordPress template files for different purposes. I will display the code snippet with an explanation of how it's used, what it does, and why you would want to use it. The goal of WordPressGarage is to be an online WordPress manual ... View Post

Showing different sidebar on different pages

| February 21, 2007 | 11 Comments
To show a different sidebar on different pages, replace the standard call for the sidebar: <?php get_sidebar(); ?> with the following code: <?php include ('sidebar2.php'); ?> The same can be done to call different headers, i.e. instead of <?php get_header(); ?> use <?php include ('header2.php'); ?>For more information, see Different Sidebars Anyone? on the ... View Post
Page 3 of 3123