用WP短代碼在側欄小工具中調用不同分類的文章

本文介紹如何簡單的在Wordpress主題中用短代碼(shortcode)在側欄的小工具裏調用不同分類的若干最新文章。

This article allow you know how to use WordPress shortcode atts.Just post on Suoling.net,Plz don't Ctrl+C and V.
1.在Wordpress主題functions.php中添加短代碼

    add_shortcode('slnet', 'slnet_shortcode');   

 
2.在Wordpress主題functions.php中添加函數

    function slnet_shortcode($atts, $content = null){   
        
        extract(shortcode_atts(array(    
        "slug"  => 'chengjiao',   
        //"title" => '最新文章',   
        //"num"   => '5',    
        ), $atts));   
                slnet_shortcode_base($slug,$title);   
                   
    }  

the function above use extract() to extract  the atts of WordPress shortcode defined by yourself.

extract() 函數用於解析短代碼的屬性並設置屬性默認值,它的一個功能是把各個屬性參數值賦給一個形如 "$參數名" 的變量保存起來(如上面例子中的 $slug ),該函數與 shortcode_atts() 配合可以很安全的輸出結果。另外,它會將短代碼中大寫的屬性值先轉換爲小寫,再做爲變量名使用,所以,短代碼中的屬性就別用大寫了,爲了提高效率,節約資源。

三個參數:分類slug、分類標題和調取的最新文章的數量,下面寫出其它函數,顯示調用內容的函數由於在具體的主題中使用,在css上有區別。

 

查詢函數,

    //Custom shortcode for widget by Suoling.net 2013.11.11    
    function coolwp_posts($query_text,$num) {   
                            $slug = $query_text;   
                            $icat=get_category_by_slug($slug);    
                            $icat_links=get_category_link($icat->term_id);    
                            $icat_name=$icat->name;      
                            $cat_id=$icat->term_id;    
            return  query_posts("cat=($cata_id)&showposts=5&orderby=date&order=DESC");   
    }  

 

顯示函數

    function echo_posts($mytitle){   
      
            echo'<h5 class="widget_title">';   
               
            if(!(strlen($mytitle)==0)){   
            echo $mytitle;   
            }else{echo '索凌網絡';}   
               
            echo'</h5><ul class="sl_latest_news_list">';   
             if (have_posts()) :  while (have_posts()) : the_post();    
                echo '<li class="sl-recent-post"><a href="';?><?php the_permalink();?><?php echo'" rel="bookmark" class="recent-post-title" title="'. get_the_title().'">'.get_the_title().'</a><span class="latest_posts_data">'?><?php the_time('Y-m-d');?><?php echo'</span></li>';   
                 endwhile;    
                     endif;    
                    wp_reset_query();    
                   
                echo '</ul>';   
      
    }  

上面的CSS需要你自己定義的哦!

 

判斷一個字符串變量是否被傳值,最好用其長度判斷:

    if(!(strlen($mytitle)==0)){      
          echo $mytitle;      
          }else{echo '最新文章';}     

 

上面的函數用作查詢,下面的函數是短代碼用的

    function slnet_shortcode_base($myslug,$mytitle){   
            coolwp_posts($myslug);   
            echo_posts($mytitle);   
    }  

 

在文本小工具中調用的短代碼:

    [slnet title="分類名稱" slug="分類別名" num="調用多少篇最新文章"][/slnet]  

 

順便說一句,你要先確認你的主題的小工具是否支持短代碼,如果不支持,請在主題的functions.php中添加如下hook:

    add_filter('widget_text', 'do_shortcode');  

 

本想把調用文章的數量也加上的,想一下,沒什麼必要,就不用了。後來,有對此函數做了修改,調用方式爲:

    [slnet  slug="分類別名"]分類名稱[/slnet]    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章