2012-11-29 53 views
1

我有點卡住了。我正在使用的模板具有稱爲產品的自定義帖子類型。當我在這裏添加帖子/頁面時,我可以使用頁面中的小部件代碼來顯示它們。wordpress部件和帖子內容問題

窗口小部件顯示輸入到自定義字段中的內容,但我不能爲了我的生活而弄清楚如何修改代碼以顯示來自custompost類型內容部分的內容。任何見解?

這是生成窗口小部件的shortcodes.php文件中的代碼。

$count_posts = wp_count_posts('product'); 
    $published_posts = $count_posts->publish; 
    $out =''; 
    $out .=' <div class="pricing-table">'; 
     $out .= '<ul>'; 
     $counter = 0; 
     while (have_posts()) : the_post(); 
     $counter++; 
     $product_price = get_post_meta($post->ID,'_product_price',true); 
      $out .= '<li class="heading-column '.$class_heading.' color'.$counter.'">'; 
      $out .= '<h3>'.get_the_title().'</h3>'; 
      $out .= '<h5>'; 
      $out .= $currency ? $currency : "&#36;"; 
      $out .= $product_price; 
      if ($billing_cycle == "none") { 
       $out .= ""; 
      } else { 
       $out .= ' per '.$billing_cycle; 
      } 
      $out .= '</h5>'; 
      $out .= '</li>'; 
     endwhile; 
     $out .= '</ul>'; 

     $out .= '<div class="clear"></div>'; 
     $out .= '<ul>'; 
     while (have_posts()) : the_post(); 
     $product_url = get_post_meta($post->ID,'_product_url',true); 
     $product_feature = get_post_meta($post->ID,'_product_feature',true); 
     $features_list = explode(",",$product_feature); 
     $counter++; 
      $out .= '<li class="pric-column '.$class_column; 
      if ($counter%$columns==0) $out .= '-last'; 
      $out .= '">'; 
      foreach ($features_list as $flist) { 
      $out .= '<ul class="feature-list">'; 
       $out .= '<li>'.$flist.'</li>'; 
      } 
      $out .= '<li class="last">'; 
      $out .= '<a class="button" href="'.$product_url.'"><span>'.$product_button_text.'</span></a>'; 
      $out .= '</li>'; 
      $out .= '</ul>'; 
     endwhile;wp_reset_query(); 
     $out .= '</ul>'; 
    $out .= '</div>'; 

    return '[raw]'.$out.'[/raw]'; 
} 

回答

0

您的代碼不起作用,因爲the loop未被填充。
那裏它會做頁面的默認循環。 (如果它已經不是已經運行過的)

另外你做的東西不會像$published_posts一樣。

您應該使用WP_Query創建自定義循環。

有一點幫助,讓你開始:

$query_args = array(
    'post_type' => 'products', // the post type name 
    'post_status' => 'publish', 
); 
$products = new WP_Query ($query_args) 

// before loop stuff like opening tags 
while($products->have_posts()): $products->the_post(); 
    // do your stuff like display post_meta 
endwhile; 
// after loop stuff like closing tags 

這應該讓你開始。
如果您需要任何幫助,請告訴我!