sticky
closeddifferent single template per category
create each different layouts you’d like to use. In this recipes, they’re named single1.php and single2.php. The default posts template is single_default.php.
insert the following code on your regular single.php file.
<?php
$post = $wp_query->post;
if (in_category('1')) {
include(TEMPLATEPATH.'/single1.php');
} elseif (in_category('2')) {
include(TEMPLATEPATH.'/single2.php');
} else {
include(TEMPLATEPATH.'/single_default.php');
}
?>
Create a dynamic sidebar
specify a sidebar name to be inclued:
<?php get_sidebar('name'); ?>
The above code will include the file named sidebar-name.php.
The following code will include a custom sidebar according to the category you are on:
<?php
//to be able to use this outside the loop
if ( have_posts() ) { the_post(); rewind_posts(); }
if ( in_category('1') ) {
get_sidebar('cat1');
//gets sidebar-cat1.php
} elseif ( in_category('2') ) {
get_sidebar('cat2');
//gets sidebar-cat2.php
} elseif ( in_category('3') ) {
get_sidebar('cat3');
//gets sidebar-cat3.php
} elseif ( in_category('4') || in_category('5') || in_category('6') ) {
get_sidebar('catRest');
//gets sidebar-catRest.php
} else {
get_sidebar()
//gets sidebar.php
}
?>
Display your latest twitter entry on your WP blog
paste the code below anywhere on your blog.
<?php
// Your twitter username.
$username = "TwitterUsername";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "<h2>My last Tweet</h2>";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace("<", "<", $tweet);
$tweet = str_replace(">", ">", $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>
List posts by author
paste the following code where you want to list the post from a specific author:
$numposts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = 1");
echo "Posts by Author 1";
< ul >
foreach ($numposts as $numpost) {
echo "< li >".$numpost->post_title."< /li >";
}
< /ul >
Control post revisions
edit your wp-config.php file.
To limit post revisions, just add the following lines:
define ('WP_POST_REVISIONS', 5); //Defines a maximum of 5 different revisions per post.
define('AUTOSAVE_INTERVAL', 3600); // Auto-saves on 1 hour interval
Change author attribution on all posts
use phpmyadmin with the following commands to modify your WordPress database, don’t forget to create a backup before running any command throught phpmyadmin.
The first thing to do is getting the IDs of WordPress users. Once logged in phpmyadmin, insert the following SQL command:
SELECT ID, display_name FROM wp_users;
Right now, phpmyadmin displayed a list of WordPress users associated with their IDs. Let’s say that NEW_AUTHOR_ID is the ID of the “new” author, and OLD_AUTHOR_ID is the old author ID.
UPDATE wp_posts SET post_author=NEW_AUTHOR_ID WHERE post_author=OLD_AUTHOR_ID;
Add a rss link on each post
edit the single.php file from your theme.
Locate the WordPress loop, and simply add the link below.
<?php if (have_posts()) :
while (have_posts()) : the_post();
// WordPress loop ?>
<div class="rss-box">< a href= " http://feeds.feedburner.com/ADRESS">Enjoyed this post? Suscribe to my RSS feeds!< /a ></div>
<?php endwhile; ?>
<?php endif; ?>
Limit the size of the post excerpt
Edit your single.php file and replace the the_excerpt() function by the following code:
<?php
$len = 50; //Number of words to display in excerpt
$newExcerpt = substr($post->post_excerpt, 0, $len); //truncate excerpt according to $len
if(strlen($newExcerpt) < strlen($post->post_excerpt)) {
$newExcerpt = $newExcerpt."[...]";
}
echo "<p>".$newExcerpt."</p>"; //finally display excerpt
?>
This topic has been closed to new replies.