elegant themes

Category: Code Snippets

Giving each comment its own link in WordPress

| November 28, 2007 | 4 Comments
Being able to link to specific comments in your WordPress blog could be very handy. Good comments can really add a lot of value to a blog, and you or others may want to refer to specific comments in your posts by linking to them. In general, comments can be linked to, but to ... View Post

Giving each WordPress post a thumbnail, and display the thumbnail on the home page

| November 21, 2007 | 195 Comments
// You may encounter situations where you need to automatically display a thumbnail or image of some kind that will link to posts. Here's an example of this kind of situation: ... View Post

Hard-coding an SEO friendly title in the WordPress header file

| November 12, 2007 | 5 Comments
The title I am referring to here is the title that appears in the top bar of your browser, and is in betweeen <title></title> tags in your code. Apparently, to make it more SEO friendly, the title of the post or article should appear before the title of your blog, but generally themes have ... View Post

Display recent posts in WordPress sidebar

| November 11, 2007 | 67 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 | 97 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
Page 3 of 41234