web/wp-content/plugins/related-posts-by-category/related_posts.php
changeset 136 bde1974c263b
child 123 561aa6d282f6
child 194 32102edaa81b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Related Posts by Category
       
     4 Plugin URI: http://playground.ebiene.de/400/related-posts-by-category-the-wordpress-plugin-for-similar-posts/
       
     5 Description: The WordPress Plugin for similar posts grouped by category. It's small. It's fast. Really.
       
     6 Author: Sergej M&uuml;ller
       
     7 Version: 0.4
       
     8 Author URI: http://www.wpseo.org
       
     9 */
       
    10 
       
    11 
       
    12 function related_posts_by_category($params, $post_id = 0) {
       
    13 $entries = array();
       
    14 $output = '';
       
    15 $limit = 0;
       
    16 $type = '';
       
    17 $order = '';
       
    18 $orderby = '';
       
    19 $post_id = intval($post_id);
       
    20 if (!$post_id) {
       
    21 $post_id = $GLOBALS['post']->ID;
       
    22 }
       
    23 if (isset($params['limit'])) {
       
    24 $limit = intval($params['limit']);
       
    25 }
       
    26 if (isset($params['type']) && !empty($params['type'])) {
       
    27 $type = ($params['type'] == 'page' ? 'page' : 'post');
       
    28 }
       
    29 if (isset($params['order']) && !empty($params['order'])) {
       
    30 $order = (strtoupper($params['order']) == 'DESC' ? 'DESC' : 'ASC');
       
    31 }
       
    32 if (isset($params['orderby']) && !empty($params['orderby']) && preg_match('/^[a-zA-Z_]+$/', $params['orderby'])) {
       
    33 $orderby = $params['orderby'];
       
    34 }
       
    35 $entries = $GLOBALS['wpdb']->get_results(
       
    36 sprintf(
       
    37 "SELECT DISTINCT object_id, post_title FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t, {$GLOBALS['wpdb']->posts} p WHERE t.term_id IN (SELECT t.term_id FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t WHERE r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'category' AND r.object_id = $post_id) AND r.term_taxonomy_id = t.term_taxonomy_id AND p.post_status = 'publish' AND p.ID = r.object_id AND object_id <> $post_id %s %s %s",
       
    38 ($type ? ("AND p.post_type = '" .$type. "'") : ''),
       
    39 ($orderby ? ('ORDER BY ' .(strtoupper($params['orderby']) == 'RAND' ? 'RAND()' : $orderby. ' ' .$order)) : ''),
       
    40 ($limit ? ('LIMIT ' .$limit) : '')
       
    41 ),
       
    42 OBJECT
       
    43 );
       
    44 if ($entries) {
       
    45 foreach ($entries as $entry) {
       
    46 $output .= sprintf(
       
    47 '%s<a href="%s" %s title="%s">%s%s%s</a>%s',
       
    48 isset($params['before']) ? $params['before'] : '',
       
    49 get_permalink($entry->object_id),
       
    50 (isset($params['rel']) ? ('rel="' .$params['rel']. '"') : ''),
       
    51 str_replace('"', '&quot;', $entry->post_title),
       
    52 isset($params['inside']) ? $params['inside'] : '',
       
    53 $entry->post_title,
       
    54 isset($params['outside']) ? $params['outside'] : '',
       
    55 isset($params['after']) ? $params['after'] : ''
       
    56 );
       
    57 }
       
    58 } else {
       
    59 $output = $params['message'];
       
    60 }
       
    61 if (isset($params['echo']) === true && $params['echo']) {
       
    62 echo $output;
       
    63 } else {
       
    64 return $output;
       
    65 }
       
    66 }
       
    67 ?>