WordPress Post Hacks01. How to List Your Upcoming Posts
<div id="zukunft"> 02. How to List Your Most Popular Posts
NOTE: Just change the “5″ in line 3 to change the number of displayed popular posts.
<h2>Popular Posts</h2> 03. How to Display Related Posts
To implement this hack you just need to open the single.php file in your theme and pasted following code within the loop. This code will display related posts based on the current post tag(s).
<?php //for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { 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; } } ?> Comment Page Related Hacks04. Displaying Recent Comments
<?php function recent_comments($src_count=10, $src_length=60, $pre_HTML='<ul>', $post_HTML='') { global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, SUBSTRING(comment_content,1,$src_length) 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 = '' ORDER BY comment_date_gmt DESC LIMIT $src_count"; $comments = $wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) { $output .= "<li><a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" . strip_tags($comment->com_excerpt) ."...</a></li>"; } $output .= $post_HTML; echo $output; } ?> Now, wherever you want to show the list of recent comments, just add this code.
<?php recent_comments(); ?> 05. How to Display The Most Commented Posts in Specific Period
To display a list of your 10 most commented posts from some specific period, simply paste the following code on your blog sidebar, or anywhere on your theme. NOTE: just change the “2008-01-01″ and “2008-12-31″ in line 4 to your desired starting and end date respectively to display the desired posts.
<h2>Most commented posts from 2008</h2> <ul> <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '2008-01-01' AND '2008-12-31' ORDER BY comment_count DESC LIMIT 0 , 10"); 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> 06. How to Separate WordPress Comments and Trackbacks
In order to separate the comments and trackbacks, you just need to follow these steps. Open comments.php, and search for the following line
<?php foreach ($comments as $comment) : ?> After it, paste the following
<?php $comment_type = get_comment_type(); ?> <?php if($comment_type == 'comment') { ?> Now look for
<?php endforeach; /* end for each comment */ ?> And before it, paste
<?php } else { $trackback = true; } /* End of is_comment statement */ ?> Now your list of comments will continue to display as normal, but without any trackbacks or pingbacks. Now we will add a second comments loop for the trackbacks.Look for the following line
<?php else : // this is displayed if there are no comments so far ?> And before it, paste this: (The “Trackbacks” title line can be deleted if you don’t want a heading to be shown)
<?php if ($trackback == true) { ?> <h3>Trackbacks</h3> <ol> <?php foreach ($comments as $comment) : ?> <?php $comment_type = get_comment_type(); ?> <?php if($comment_type != 'comment') { ?> <li><?php comment_author_link() ?></li> <?php } ?> <?php endforeach; ?> </ol> <?php } ?> |
WordPress Post Hacks

















