Login

Login



You are here!  Home WordPress Post Hacks



WordPress Post Hacks

01. How to List Your Upcoming Posts

qwh-01


If you want to display your schedule posts in a list which can often help readers to give them heads up about what you're going to publish in upcoming days. This can also help you to get new visitors and some more RSS subscribers. It’s not very hard to implement this functionality as it seems. You just need to simply paste this code where you’d like your future posts to be displayed.

<div id="zukunft">
<div id="zukunft_header"> Future events </div>
<?php query_posts('showposts=10&post_status=future'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div> <p class=""><b><?php the_title(); ?>
</b>
<?php edit_post_link('e',' (',')'); ?>
<span class="datetime"><?php the_time('j. F Y'); ?></span>
</p></div>
<?php endwhile; else: ?> No future events scheduled. <?php endif; ?> </div>

02. How to List Your Most Popular Posts


qwh-02

There are many ways by which you can show popular posts to your readers. It’s been quite a simple to install a plugin to display your most popular posts. But if you want to avoid plugins and want to create a list for your most popular post then you just need to paste the following code anywhere in your theme files. If you want to show popular posts in your sidebar section then simply go to sidebar.php and paste the code.

NOTE: Just change the “5″ in line 3 to change the number of displayed popular posts.

<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5"); 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); ?>" title="<?php echo $title ?>"> <?php echo $title ?></a> {<?php echo $commentcount ?>}</li> <?php } } ?>
</ul>

03. How to Display Related Posts

qwh-03

Related posts are often helpful to make your readers stay much longer on your website as they offer them to easily navigate related content via simple clicks.

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 Hacks

04. Displaying Recent Comments

instantShift - Quality Wordpress Hacks

Recent comments are very helpful for readers to comment on any topic about any ongoing discussion. To display your most recent comments, you will have to modify your functions.php file from the theme folder. If the functions.php file is not present, make a new text file, name it functions.php and add it to your theme folder. Add the following code to it and save the file.

<?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

instantShift - Quality Wordpress Hacks

Many time it’s rather helpful to show most commented posts on your blog to make things easy for readers in selecting the most controversial or popular posts.

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

instantShift - Quality Wordpress Hacks

Trackbacks are the messages displayed in the comments list whenever another blog links back to one of your posts. If you use trackbacks on your blog, it is best if they are not mixed with the comments. The comments are a conversation between real people. Having machine-generated links in the middle of that will only serve to disrupt the conversations.

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 } ?>




GrowingFoundations
WEB DESIGN STATES & CITIES How we can help in your area!
HYBRID WEBSITE MARKETING Tailored your niche Market!
DEVELOPER TIPS AND TRICKS Refine your skills...