Pages

2010年11月4日 星期四

文章新增欄位外掛custom field template

參考網站http://www.kevinleary.net/advanced-content-management-wordpress-custom-field-templates/



Add The get­Cus­tom­Field() Func­tion to Functions.php

Open up your theme’s functions.php file. If you don’t have one, cre­ate it. Add the fol­low­ing PHP func­tion to the file.
// Get Custom Field Template Values
function getCustomField($theField) {
 global $post;
 $block = get_post_meta($post->ID, $theField);
 if($block){
  foreach(($block) as $blocks) {
   echo $blocks;
  }
 }
}
 
 
 
 

Load The Con­tent into Your Theme

Next we need to take the con­tent cre­ated by these new fields and add it to our tem­plate. To do this we’ll need some basic PHP.
<div id="block-1" class="content-box">
 <h2>Block #1</h2>
 <div class="entry">  
  <?php getCustomField('Block #1'); ?>   
 </div>
</div>
<div id="block-2" class="content-box">
 <h2>Block #2</h2>
 <div class="entry">  
  <?php getCustomField('Block #2'); ?>   
 </div>
</div>

沒有留言:

張貼留言