不用外掛使用隨機文章、相關文章

转自点点 14 年前 回复

, , , , ,

隨機文章:
前台呼叫語法:

<?php s_random_lists(); ?>

以下內容加入functions.php

<?php
function s_random_lists($num_limit = 5 , $exclude = "" , $date_limit = "" , $echo = true , $list = true){
        $out = "";
        if ( $num_limit < 1 ) $num_limit = "-1";
        if ( !$date_limit_ts = strtotime($date_limit) ) $date_limit = false;
        if ( !$date_limit ){
            $posts = get_posts('offset=0&numberposts='.$num_limit.'&exclude='.$exclude.'&orderby=rand');
        } else {
            $posts = get_posts('offset=0&numberposts=-1&exclude='.$exclude.'&orderby=rand');
        }
        $postscount = count($posts);
        if ( $num_limit < 1 ) $num_limit = $postscount;
        if ( $postscount < $num_limit ) $num_limit = $postscount ;
        for ( $i = 0 ; $i < $num_limit ; $i++ ){
             if ( !$date_limit or $date_limit_ts < strtotime( $posts[$i]->post_date )){
                if ( $list ) $out.= '<li class="random-post-link">'."\n";
                  $out.= '<a href="'.get_permalink($posts[$i]->ID).'" title="'.$posts[$i]->post_title.'">'.$posts[$i]->post_title.'</a>'."\n";
                if ( $list ) $out.= '</li>'."\n";
            }else{
                if ( $postscount > $num_limit ) $num_limit++;
            }
        }
        if ( $list ) $out = '<ul class="random-post-link">'."\n".$out.'</ul>'."\n";
        if ( $echo ){
              echo $out;
        } else {
            return $out;
        }
    }
?>

顯示文章數量設定:

$num_limit = 5 ,修改為要顯示的數量即可。

相關文章:
前台呼叫:

<?php s_related_posts(); ?>

以下內容加入functions.php

<?php
function srelated_posts($before_title="",$after_title="") {    
    global $wpdb, $post,$table_prefix;
    if(!$post->ID){return;}
    $now = current_time('mysql', 1);
    $tags = wp_get_post_tags($post->ID);
    
    $taglist = "'" . $tags[0]->term_id. "'";
    
    $tagcount = count($tags);
    if ($tagcount > 1) {
        for ($i = 1; $i < $tagcount; $i++) {
            $taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
        }
    }    
    $limitclause = "LIMIT 5";    
    $q = "SELECT p.ID, p.post_title, p.post_content,p.post_excerpt, p.post_date,  p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id  = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";    
    $related_posts = $wpdb->get_results($q);
    foreach ($related_posts as $related_post ){
        $output .=  '<li><a href="'.get_permalink($related_post->ID).'" title="'.wptexturize($related_post->post_title).'">'.wptexturize($related_post->post_title).'</a></li>';            
    }    
    $output = '<ul class="related_post">' . $output . '</ul>';        
    return $output;
}
function s_related_posts(){
    $output = srelated_posts() ;
    echo $output;
}
?>

顯示文章數量設定:

$limitclause = "LIMIT 5";,修改為要顯示的數量即可。

原文http://steachs.com/archives/1406

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)