Pages

7/19/2012

Wordpress : Tricks

รวมเทคนึกการเขียน Wordpress ที่ได้ไปเจอมา เอามาแชร์กันบ้าง เผื่อบางคนไม่รู้

1. เวลาใช้คำสั่งดึง Category ทั้งหมด โดยที่บาง Category ไม่มี Content อยู่ปกติจะไม่สามารถดึงขึ้นมาได้ ตัวอย่างเช่น

$args = "orderby=ID&exclude=1&hide_empty=0";
$categories = get_categories($args);

ให้เติม hide_empty=0 เข้าไปก็จะสามารถแสดง Category ที่ไม่มี Content ในหมวดนั้นๆขึ้นมาได้.. จบปะ
More Detail : http://codex.wordpress.org/Template_Tags/wp_list_categories

2.ทำ Random Content ขึ้นมาแสดง
 <?php  
 query_posts(array('orderby' => 'rand', 'showposts' => 1));  
 if (have_posts()) :  
 while (have_posts()) : the_post(); ?>  
 <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>  
 <?php the_content(); ?>  
 <?php endwhile;  
 endif; ?>  

3. ดึง Content RSS -> HTML

 <?php include_once(ABSPATH.WPINC.'/feed.php');  
 $rss = fetch_feed('http://feeds.feedburner.com/wpbeginner');  
 $maxitems = $rss->get_item_quantity(5);  
 $rss_items = $rss->get_items(0, $maxitems);  
 ?>  
 <ul>  
 <?php if ($maxitems == 0) echo '<li>No items.</li>';  
 else  
 // Loop through each feed item and display each item as a hyperlink.  
 foreach ( $rss_items as $item ) : ?>  
 <li>  
 <a href='<?php echo $item->get_permalink(); ?>'  
 title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>  
 <?php echo $item->get_title(); ?></a>  
 </li>  
 <?php endforeach; ?>  
 </ul>  

4. ดึง Content Twitter เอามาแสดง ในหน้าเวป

 <?php  
 $tw = get_option("twitterfollowerscount");  
 if ($tw['lastcheck'] < ( mktime() – 3600 ) )  
 {  
 $xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=wpbeginner');  
 if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {  
 $tw['count'] = $match[1];  
 }  
 $tw['lastcheck'] = mktime();  
 update_option("twitterfollowerscount",$tw);  
 }  
 echo $tw['count'];  
 ?>   

5. ลบ Posts Revisions from Your Database

DELETE FROM wp_posts WHERE post_type = "revision";

6. ทำ Banner แบบ Peel ไม่ยาก อ่านที่นี่ ... peel away

7. ดึง Last Sticky Post

 <?php  
 /* Get all sticky posts */  
 $sticky = get_option( 'sticky_posts' );  
 /* Sort the stickies with the newest ones at the top */  
 rsort( $sticky );  
 /* Get the 5 newest stickies (change 5 for a different number) */  
 $sticky = array_slice( $sticky, 0, 5 );  
 /* Query sticky posts */  
 query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );  
 ?>  

8. จับ Category ลง Dropdown

 <form action="<?php bloginfo('url'); ?>/" method="get">  
 <?php  
 $select = wp_dropdown_categories('show_option_none=Select Category&show_count=1&orderby=name&echo=0&selected=6');  
 $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);  
 echo $select;  
 ?>  
 <noscript><input type="submit" value="View" /></noscript>  
 </form>   

9. สร้าง Relate Post ไม่ใช่ Plug-in

 <?php  
 //for use in the loop, list 5 post titles related to first tag on   
 //current post  
 $tags = wp_get_post_tags($post->ID);  
 if ($tags) {  
  echo 'Related Posts';  
  $first_tag = $tags[0]->term_id;  
  $args=array(  
   'tag__in' => array($first_tag),  
   'post__not_in' => array($post->ID),  
   'showposts'=>5,  
   'caller_get_posts'=>1  
   );  
  $my_query = new WP_Query($args);  
  if( $my_query->have_posts() ) {  
   while ($my_query->have_posts()) : $my_query->the_post(); ?>  
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="  
    Permanent Link to <?php the_title_attribute(); ?>">  
    <?php the_title(); ?></a></p>  
    <?php  
   endwhile;  
  }  
 }  
 ?>  

10. สร้าง Short URL ด้วย TinyURL ทำใน Function.php
 function getTinyUrl($url) {  
 $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);  
 return $tinyurl;  
 }  

    ทำใน single.php 
 <?php  
 $turl = getTinyUrl(get_permalink($post->ID));  
 echo 'Tiny Url for this post: <a href="'.$turl.'">'.$turl.'</a>'  
 ?>  


11. 

No comments:

Post a Comment