Some WordPress Tricks
POSTED IN WordPressIf you are one of the millions of people who use WordPress, this article will help you improve the appearance, functionality, and performance of your WordPress-powered websites.
Display WordPress Permalinks outside of the loop
<!-- external permalink via post ID -->
<a href="<?php echo get_permalink(16); ?>">Permalink</a>
<!-- external permalink via global $post variable -->
<a href="<?php echo get_permalink($post->ID); ?>">Permalink</a>
Make a list with all of your site’s posts
wp_get_archives(type=postbypost&limit=)
and if this function doesn’t provide enough flexibility to meet your needs, here is another way
<?php while(have_posts()) : the_post(); ?>
<ul>
<?php $allposts = get_posts('numberposts=-1&offset=0'); foreach($allposts as $post) : ?>
<li><?php the_time('d/m/y'); ?>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>
Create a custom CSS for recent posts
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
$currentdate = date('Y-m-d',mktime(0,0,0,date('m'),date('d'),date('Y')));
$postdate = get_the_time('Y-m-d');
if ($postdate == $currentdate) {
echo '<div>';
} else {
echo '<div>';
} ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<?php endwhile; endif; ?>
and css style:
.post {
/* styles for all posts */
}
.post.new {
/* styles for new posts */
}
Diasble or to limit WordPress post revisions
To disable just add the following line to the wp-config.php file
/* disable post-revisioning nonsense */
define('WP_POST_REVISIONS', FALSE);
or to limit post revision
/* limit number of post revisions */
define('WP_POST_REVISIONS', 3);
Create a good Meta Description, without any plugin
<meta name="description" content="
<?php if ((is_home()) || (is_front_page())) {
echo ('Your main description goes here');
} elseif(is_category()) {
echo category_description();
} elseif(is_tag()) {
echo 'Tag archive page for this blog - ' . single_tag_title();
} elseif(is_month()) {
echo 'Archive page for this blog - ' . the_time('F, Y');
} else {
echo get_post_meta($post->ID, 'metadescription', true);
} ?>" />
Access post data outside the loop
First, you need to insert this into functions.php
function get_post_data($postId) {
global $wpdb;
return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId");
}
and after, add this code to the desired location outside of the loop
<?php $data = get_post_data(85);
echo $data->post_date; // post date
echo $data->post_title; // post title
echo $data->post_content; // post content
echo $data->comment_count; // comments number
?>
Display performance Statistics for WordPress pages
Everyone knows what that means
<!-- 24 queries in 0.333 seconds -->
This information is generated by including the following code in your footer.php or any page you would like
<!-- <?php echo get_num_queries(); ?> queries in <?php timer_stop(3); ?> seconds -->
Display most commented posts without a plugin
<ul>
<?php
$result = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10"); // NUMBER OF POSTS
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) {
?>
<li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li>
<?php } } ?>
</ul>
Display a Custom Message on a specific day
<?php
if ((date('m') == 4) && (date('d') == 16)) { ?>
<p>Today is <a href="http://www.template-tuners.com/about-me">my birthday</a>!</p>
<?php } ?>
or probably you want to remove a script or a stylesheet on a specific day
<?php if ((date('m') !== 5) && (date('d') !== 19)) { ?><link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" /> <?php } ?>
Display the total number of users from your WordPress blog
$users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
echo $users." registered users.";
Display automatic Tynyurls for your posts
Add this into functions.php
function getTinyUrl($url) {
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
return $tinyurl;
}
Then, call the function and display the URL by placing this code into your single.php file in the desired location within the loop
<?php $turl = getTinyUrl(get_permalink($post->ID));
echo 'TinyURL for this post: <a href="'.$turl.'">'.$turl.'</a>'; ?>
Display first image for every post
Add this into functions.php
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$first_img = "/images/default.jpg"; // default image
}
return $first_img;
}
Then, to call the function and display the images as thumbnails, place the following code into the desired location within your loop
<?php echo catch_that_image(); ?>
Display Popular Posts without a plugin
<ul>
<?php $result = $wpdb->get_results("
SELECT comment_count, ID, post_title
FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 7
");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>">
<?php echo $title; ?></a> [<?php echo $commentcount ?>]</li>
<?php } ?>
<?php } ?>
</ul>
Display Ads only on first 2 posts
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($wp_query->current_post < 2) { ?>
<!-- ads here -->
<?php } ?>
<?php endwhile; else: ?>
<?php endif; ?>
Add aditional link pages to wp-list-pages
Add this into functions.php
function add_bookmarks_to_menu($output) {
$bookmarks = (array) get_bookmarks('hide_invisible=0&category_name=wp_list_pages');
foreach ($bookmarks as $bookmark) {
$output .= "<li><a href='{$bookmark->link_url}' title='{$bookmark->link_name}'>{$bookmark->link_name}</a></li>\n";
}
return $output;
}
add_filter('wp_list_pages', 'add_bookmarks_to_menu');
Once this code is in place, login to your WordPress Admin and follow these steps for each link that you would like to add to the page list:
- In the Admin, go to Links > Add New
- Enter a name and URL for your link
- Add the link to a new category called "wp_list_pages"
- select "Keep this link private"
- Click "Add Link"
- Done.
I hope these tricks will help someone!

[...] Some WordPress Tricks – Template Tuners Blog on Web Design, SEO & Online Marketing, E-Comm… [...]
Thanks very much for ‘Access post data outside the loop’!
I couldn’t find how to do this in the Codex, but your trick did the job.