5, // limit number of related posts to display 'title' => '', // the title 'beforeposts' => '', 'afterposts' => '', // text before and after the list 'eachpost' => '
  • %title%
  • ', // for each related post 'noposts' => '' // can be a string to display if there are no related posts */ //----------------------------------------------------------------------------- // get related posts function rp_get_related_posts( $post, $limit ) { global $wpdb; // wordpress database access // limit has to be a number $limit = (int)$limit; // get tags of the post $tags = wp_get_post_tags( $post->ID ); if( is_wp_error($tags) ) return false; // error if( count($tags)<=0 ) // we cannot get related posts without tags return array(); // no related posts // get term ids $termids = array(); foreach( $tags as $tag ) { $termids[ $tag->term_id ] = $tag->term_id; } if( count($termids)<=0 ) // we cannot get related posts without the termids return array(); // no related posts // the query to get the related posts $query = "SELECT DISTINCT $wpdb->posts.*, COUNT( tr.object_id) AS cnt " // get posts and count ."FROM $wpdb->term_taxonomy tt, $wpdb->term_relationships tr, $wpdb->posts " ."WHERE 1 " ."AND tt.taxonomy = 'post_tag' " // search for tags ."AND tt.term_taxonomy_id = tr.term_taxonomy_id " // get relations ."AND tr.object_id = $wpdb->posts.ID " // get posts ."AND tt.term_id IN( ".implode(',',$termids)." ) " // only with the same tags ."AND $wpdb->posts.ID != $post->ID " // only other posts, not the post selfe ."AND $wpdb->posts.post_status = 'publish' " // only published posts ."GROUP BY tr.object_id " // group by relation ."ORDER BY cnt DESC, $wpdb->posts.post_date_gmt DESC " // order by count best matches first, if same order by date ."LIMIT $limit "; // get only the top x // run the query and return the result return $wpdb->get_results( $query ); } //----------------------------------------------------------------------------- // replace placeholders function rp_replace_placeholders( $post, $string ) { // replace placeholders $string = str_replace( '%title%', get_the_title($post->ID), $string ); $string = str_replace( '%permalink%', get_permalink($post->ID), $string ); // return return $string; } //----------------------------------------------------------------------------- // get related posts of a post as string function rp_getstring_related_posts( $post, $args ) { // args $defaults = array( 'limit' => 3, // limit number of related posts to display 'title' => '', // the title 'beforeposts' => '', 'afterposts' => '', // text before and after the list 'eachpost' => '
  • %title%
  • ', // for each related post 'noposts' => '' // can be a string to display if there are no related posts ); $args = wp_parse_args( $args, $defaults ); // no posts string $noposts = ''; if( strlen($args['noposts'])>0 ) { $noposts = rp_replace_placeholders( $post, $args['title'] ) .rp_replace_placeholders( $post, $args['noposts'] ); } // get related posts $relatedposts = rp_get_related_posts( $post, $args['limit'] ); if( is_wp_error($relatedposts) || !is_array($relatedposts) ) return $noposts; // print only if there are related posts if( count($relatedposts)<=0 ) return $noposts; // the string $string = ''; // print title and before $string.= rp_replace_placeholders( $post, $args['title'] ); $string.= rp_replace_placeholders( $post, $args['beforeposts'] ); // print related posts foreach( $relatedposts as $relatedpost ) { $string.= rp_replace_placeholders( $relatedpost, $args['eachpost'] ); } // print after $string.= rp_replace_placeholders( $post, $args['afterposts'] ); // return string return $string; } // output related posts of post function rp_print_related_posts( $post, $args ) { // display if there is something to display $string = rp_getstring_related_posts( $post, $args ); if( strlen($string)>0 ) echo $string; // output done return; } //----------------------------------------------------------------------------- // output related posts for the current post function rp_related_posts( $title, $args=null ) { global $post; if( !is_array($args) ) $args = array(); $args['title'] = $title; rp_print_related_posts( $post, $args ); } //----------------------------------------------------------------------------- // find the post content relatedposts placeholder function rp_filter_the_content( $content ) { global $post; // the current post // replace placeholders $content = str_replace( '%RELATEDPOSTS%', rp_getstring_related_posts($post,array()), $content ); return $content; } //----------------------------------------------------------------------------- // the sidebar widget function rp_widget( $args ) { global $post; // the current post // check if viewing a post if( !is_single() ) // show widget only on post page return; // comment // if you dont like this comment, you may remove it :-( echo ''; // args extract( $args ); // extract args // options $options = get_option( 'rp_widget' ); // get options // get related posts string $relatedposts_string = rp_getstring_related_posts( $post, $options['args'] ); if( strlen($relatedposts_string)<=0 ) return; // nothing to display echo $before_widget; echo $before_title . $options['title'] . $after_title; echo $relatedposts_string; echo $after_widget; // output done return; } function rp_widget_control() { // options $options = $newoptions = get_option('rp_widget'); // get options // set new options if( $_POST['rp-widget-submit'] ) { $newoptions['title'] = strip_tags( stripslashes($_POST['rp-widget-title']) ); $newoptions['args']['beforeposts'] = stripslashes( $_POST['rp-widget-args-beforeposts'] ); $newoptions['args']['afterposts'] = stripslashes( $_POST['rp-widget-args-afterposts'] ); $newoptions['args']['eachpost'] = stripslashes( $_POST['rp-widget-args-eachpost'] ); $newoptions['args']['noposts'] = stripslashes( $_POST['rp-widget-args-noposts'] ); $newoptions['args']['limit'] = (int) $_POST['rp-widget-args-limit']; } // update options if needed if( $options != $newoptions ) { $options = $newoptions; update_option('rp_widget', $options); } // output echo '

    '._e('This widget only appears on post pages!').'

    '; echo '

    '._e('Title'); echo ''.'
    '; echo '

    '; echo '

    '._e('Postlist'); echo ''._e('Number of related posts to display').'
    '; echo ''._e('Output before postlist').'
    '; echo ''._e('Output after postlist').'
    '; echo ''._e('Output for each related post').'
    '; echo '

    '; echo '

    '._e('Widget'); echo ''._e('Output if there are no related posts. Leave blank to hide the Widget if there are no posts to display.').'
    '; echo '

    '; echo ''; } //----------------------------------------------------------------------------- // activate and deactivate plugin function rp_activate() { // options, defaultvalues $options = array( 'widget' => array( 'title' => 'Related Posts', 'args' => array( 'limit' => 3, 'beforeposts' => '', 'eachpost' => '
  • %title%
  • ', 'noposts' => '' ) ) ); // register option add_option( 'rp_widget', $options['widget'] ); // activeted return; } function rp_deactivate() { // unregister option delete_option('rp_widget'); // deactivated return; } // initialization function rp_init() { // register widget $class['classname'] = 'rp_widget'; wp_register_sidebar_widget('related_posts', __('Related Posts'), 'rp_widget', $class); wp_register_widget_control('related_posts', __('Related Posts'), 'rp_widget_control', 'width=300&height=500'); // initialization done return; } //----------------------------------------------------------------------------- // actions add_action( 'activate_'.plugin_basename(__FILE__), 'rp_activate' ); add_action( 'deactivate_'.plugin_basename(__FILE__), 'rp_deactivate' ); add_action( 'init', 'rp_init'); // filter text to replace relatedposts placeholder add_filter( 'the_content', 'rp_filter_the_content', 5 ); add_filter( 'the_content_rss', 'rp_filter_the_content', 5 ); add_filter( 'the_excerpt', 'rp_filter_the_content', 5 ); add_filter( 'the_excerpt_rss', 'rp_filter_the_content', 5 ); add_filter( 'widget_text', 'rp_filter_the_content', 5 ); //----------------------------------------------------------------------------- /* Plugin Name: WP-PageNavi Plugin URI: http://www.lesterchan.net/portfolio/programming.php */ function wp_pagenavi($before = '', $after = '') { global $wpdb, $wp_query; if (!is_single()) { $request = $wp_query->request; $posts_per_page = intval(get_query_var('posts_per_page')); $paged = intval(get_query_var('paged')); $pagenavi_options = get_option('pagenavi_options'); $numposts = $wp_query->found_posts; $max_page = $wp_query->max_num_pages; /* $numposts = 0; if(strpos(get_query_var('tag'), " ")) { preg_match('#^(.*)\sLIMIT#siU', $request, $matches); $fromwhere = $matches[1]; $results = $wpdb->get_results($fromwhere); $numposts = count($results); } else { preg_match('#FROM\s*+(.+?)\s+(GROUP BY|ORDER BY)#si', $request, $matches); $fromwhere = $matches[1]; $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere"); } $max_page = ceil($numposts/$posts_per_page); */ if(empty($paged) || $paged == 0) { $paged = 1; } $pages_to_show = intval($pagenavi_options['num_pages']); $pages_to_show_minus_1 = $pages_to_show-1; $half_page_start = floor($pages_to_show_minus_1/2); $half_page_end = ceil($pages_to_show_minus_1/2); $start_page = $paged - $half_page_start; if($start_page <= 0) { $start_page = 1; } $end_page = $paged + $half_page_end; if(($end_page - $start_page) != $pages_to_show_minus_1) { $end_page = $start_page + $pages_to_show_minus_1; } if($end_page > $max_page) { $start_page = $max_page - $pages_to_show_minus_1; $end_page = $max_page; } if($start_page <= 0) { $start_page = 1; } if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) { $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']); $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text); echo $before.'
    '."\n"; switch(intval($pagenavi_options['style'])) { case 1: if(!empty($pages_text)) { echo ' '.$pages_text.' '; } if ($start_page >= 2 && $pages_to_show < $max_page) { $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']); echo ' '.$first_page_text.' '; if(!empty($pagenavi_options['dotleft_text'])) { echo ' '.$pagenavi_options['dotleft_text'].' '; } } previous_posts_link($pagenavi_options['prev_text']); for($i = $start_page; $i <= $end_page; $i++) { if($i == $paged) { $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']); echo ' '.$current_page_text.' '; } else { $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']); echo ' '.$page_text.' '; } } next_posts_link($pagenavi_options['next_text'], $max_page); if ($end_page < $max_page) { if(!empty($pagenavi_options['dotright_text'])) { echo ' '.$pagenavi_options['dotright_text'].' '; } $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']); echo ' '.$last_page_text.' '; } break; case 2; echo '
    '."\n"; echo '\n"; echo "
    \n"; break; } echo '
    '.$after."\n"; } } } add_action('init', 'pagenavi_init'); function pagenavi_init() { // Add Options $pagenavi_options = array(); $pagenavi_options['current_text'] = '%PAGE_NUMBER%'; $pagenavi_options['page_text'] = '%PAGE_NUMBER%'; $pagenavi_options['first_text'] = __('« First','wp-pagenavi'); $pagenavi_options['last_text'] = __('Last »','wp-pagenavi'); $pagenavi_options['next_text'] = __('»','wp-pagenavi'); $pagenavi_options['prev_text'] = __('«','wp-pagenavi'); $pagenavi_options['dotright_text'] = __('...','wp-pagenavi'); $pagenavi_options['dotleft_text'] = __('...','wp-pagenavi'); $pagenavi_options['style'] = 1; $pagenavi_options['num_pages'] = 5; $pagenavi_options['always_show'] = 0; add_option('pagenavi_options', $pagenavi_options, 'PageNavi Options'); } // Show menu in header.php // Exlude the pages from the slider function woo_exclude_pages() { $exclude = ''; $exclude = $exclude . get_option( 'woo_tabber_pages' ) . ',' . get_option( 'woo_intro_page' ) . ',' . get_option( 'woo_intro_page_left' ) . ',' . get_option( 'woo_intro_page_right' ); return $exclude; } /*-----------------------------------------------------------------------------------*/ /* WordPress 3.0 New Features Support */ /*-----------------------------------------------------------------------------------*/ if ( function_exists('wp_nav_menu') ) { add_theme_support( 'nav-menus' ); register_nav_menus( array( 'primary-menu' => __( 'Primary Menu' ) ) ); register_nav_menus( array( 'footer-menu' => __( 'Footer Menu' ) ) ); } /*-----------------------------------------------------------------------------------*/ /* Subscribe / Connect */ /*-----------------------------------------------------------------------------------*/ if (!function_exists( 'woo_subscribe_connect')) { function woo_subscribe_connect($widget = 'false', $title = '', $form = '', $social = '') { global $woo_options; // Setup title if ( $widget != 'true' ) $title = $woo_options[ 'woo_connect_title' ]; // Setup related post (not in widget) $related_posts = ''; if ( $woo_options[ 'woo_connect_related' ] == "true" AND $widget != "true" ) $related_posts = do_shortcode( '[related_posts limit="5"]' ); ?>

    >

    ">