Useful WordPress Hacks
Show related post without a plugin
If you want to show related post in you current post , you can use simple way to show. You just need to copy and paste under following codes into single.php file of your theme. This code will display related posts based on the current post tag(s). It must be pasted within the loop.
<?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;
}
}
?>
source : How to: Show related posts without a plugin
Create WordPress Shortcode for related posts
You can use WordPress shortcode too.
To create the shortcode, simply open your functions.php file and paste the shortcode function:
function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
'limit' => '5',
), $atts));
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval = '<ul>';
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(',', $tagsarray);
// Do the query
$q = "SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = 'publish'
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;";
$related = $wpdb->get_results($q);
if ( $related ) {
foreach($related as $r) {
$retval .= '
<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>
';
}
} else {
$retval .= '
<li>No related posts found</li>
';
}
$retval .= '</ul>
';
return $retval;
}
return;
}
add_shortcode('related_posts', 'related_posts_shortcode');
Once done, you can use the following shortcode in your posts to display the related content:
[related_posts] [/pre> Source : WordPress shortcode to display related postsShow related post from same category
If you want to get related post base on category , you can simply add some code in function.php file. It displays posts depending of the category. This function have three parameters -
- $limit (int) amount of posts to display
- $catName (bool) echo category name, TRUE or FALSE
- $title (string) String for a text before all entries
/**
* related post with category
* @param: int $limit limit of posts
* @param: bool $catName echo category name
* @param: string $title string before all entries
* Example: echo fb_cat_related_posts();
*/
if ( !function_exists('fb_get_cat_related_posts') ) {
function fb_get_cat_related_posts( $limit = 5, $catName = TRUE, $title = '<h3>Recent Pages</h3>' ) {
if ( !is_single() )
return;
$limit = (int) $limit;
$output = '';
$output .= $title;
$category = get_the_category();
$category = (int) $category[0]->cat_ID;
if ( $catName )
$output .= __( 'Kategorie: ' ) . get_cat_name($category) . ' ';
$output .= '<ul>';
$args = array(
'numberposts' => $limit,
'category' => $category,
);
$recentposts = get_posts( $args );
foreach($recentposts as $post) {
setup_postdata($post);
$output .= '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
}
$output .= '</ul>';
return $output;
}
}
[/pre>
Source : Related Posts on Category
Display comments and trackbacks separately
Let's face it: When you're reading comments on a blog post, trackbacks are annoying. It's way better to display it separately from comments.
Open and edit the comments.php file from your theme.
Find the comment loop:
foreach ($comments as $comment) : ?>
// Comments are displayed here
endforeach;
Replace it with the following:
<ul>
<?php //Displays comments only
foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>
<li>//Comment code goes here</li>
<?php }
endforeach;
</ul>
<ul>
<?php //Displays trackbacks only
foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
<li><?php comment_author_link() ?></li>
<?php }
endforeach;
</ul>
Soruce : Jamie asked: “How can I display comments and trackbacks separately?”
List Future Posts
If you want to display list of future post what you scheduled to be publish, simply paste this code where you would like to be displayed.
<div id="zukunft">
<div id="zukunft_header"><p>Future events</p></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',' (',')'); ?><br />
<span><?php the_time('j. F Y'); ?></span></p>
</div>
<?php endwhile; else: ?><p>No future events scheduled.</p><?php endif; ?>
</div>
Source : How to: List future posts
Show popular posts without a plugin
If you want to show popular post in your post or side bar, you just paste following code where you would like to displayed.
<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>
Source : Create Your Own Popular Post Page
Highlight Searched Text In Search Results
If you want to highlight searched text in search resualt, you can get after following step.
Open your search.php file and find the the_title() function. Replace it with the following:
echo $title;
Now, just before the modified line, add this code:
<?php
$title = get_the_title();
$keys= explode(" ",$s);
$title = preg_replace('/('.implode('|', $keys) .')/iu',
'<strong>