隨著Wordpress愈來愈風行,隨之而起的外掛也愈來愈多,一不小心隨便裝都2.30個外掛,也有看過有人裝了50個以上,不過外掛裝的愈多愈耗記憶體,也容易拖慢網頁速度,所以能不用外掛就盡量不要用外掛,特別是一些比較簡易的功能像是《隨機文章、相關文章、文章分頁》這些其實如果在製作佈景時就可以順便加進去,減輕網站的一些負擔。

00

以下的代碼不像別人所分享的是直接將全部代碼加在佈景裡,而是先在functions裡寫好函數再到佈景裡調用,這樣在佈景裡看到的函數就不會感覺亂亂的,往後比較好修改,有需要的可以參考,丫湯也是能整合到佈景裡的功能都盡量整合,目前使用的外掛大約10幾個。

 

隨機文章:

前台呼叫語法:

<?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";,修改為要顯示的數量即可。

 

文章分頁:

前台呼叫:

<div id="spagenavi"><?php s_pagenavi(); ?></div>

CSS部份(style.css):

#paviinfo{border: 3px double #bfbfbf;padding:2px 5px;margin:0 2px;float:left;text-align:center;}

#spagenavi{float:right;font-size:13px;font-family:Arial;display:inline-block;}

#spagenavi a{color: #333;float:left;padding:2px 5px;border: 3px double #bfbfbf;margin:0 2px;text-decoration:none;}

#spagenavi a:hover{border-color: #BA0000;}

#spagenavi a.current{border-color: #0075b8;}

以下內容加入functions.php

<?php

    function s_pagenavi($range = 6){

  global $paged, $wp_query;

  if ( !$max_page ) {

    $max_page = $wp_query->max_num_pages;

  }

  if($max_page > 1){

    if(!$paged){

      $paged = 1;

    }

echo '<div id="paviinfo">文章頁次'.$paged.'/'.$max_page.'</div>';

    if($paged != 1){

      echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='首頁'>首頁</a>";

    }

    previous_posts_link(' « ');

    if($max_page > $range){

      if($paged < $range){

        for($i = 1; $i <= ($range + 1); $i++){

          echo "<a title=".'"第'.$i.'頁"'. "href='" . get_pagenum_link($i) ."'";

          if($i==$paged) echo " class='current'";

          echo ">$i</a>";

        }

      }

      elseif($paged >= ($max_page - ceil(($range/2)))){

        for($i = $max_page - $range; $i <= $max_page; $i++){

          echo "<a title=".'"第'.$i.'頁"'. "href='" . get_pagenum_link($i) ."'";

          if($i==$paged) echo " class='current'";

          echo ">$i</a>";

        }

      }

      elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){

        for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){

          echo "<a title=".'"第'.$i.'頁"'. "href='" . get_pagenum_link($i) ."'";

          if($i==$paged) echo " class='current'";

          echo ">$i</a>";

        }

      }

    }

    else{

      for($i = 1; $i <= $max_page; $i++){

        echo "<a title=".'"第'.$i.'頁"'. "href='" . get_pagenum_link($i) ."'";

        if($i==$paged) echo " class='current'";

        echo ">$i</a>";

      }

    }

    next_posts_link(' » ');

    if($paged != $max_page){

      echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='最後一頁'>最後一頁</a>";

    }

  }

}

    ?>

文章分頁效果:

01

Categorized in:

WordPress,

Last Update: 2010 年 01 月 10 日