2013-05-06 93 views
2

我有一個「職員」的自定義職位類型。該帖子類型具有「職位」(醫生,護士等)的分類。我正在嘗試創建一個簡短代碼,允許用戶只返回頁面上特定分類的工作人員。WordPress短代碼添加分類屬性

例子:

[staff position="doctors"] 

我試過以下幾個教程,但似乎無法得到這個工作。這是我迄今爲止的代碼,它返回所有員工。

function get_staff($atts) { 
$loop = new WP_Query(
    array (
     'post_type' => 'staff', 
     'orderby' => 'title' 
    ) 
); 

if ($loop->have_posts()) { 
    $output = '<div class="staff">'; 

    while($loop->have_posts()){ 
     $loop->the_post(); 
     $meta = get_post_meta(get_the_id()); 

     $output .= ' 
      <div class="staff" style="float: left; display: block; border: 1px solid #CCC; margin: 10px; padding: 12px; background-color: #eee;"> 
       <a href="' . get_permalink() . '"> 
        ' . get_the_post_thumbnail($post->ID, 'thumbnail') . '<br /> 
       ' . get_the_title() . '</a><br /> 
       ' . get_the_excerpt() . ' 
      </div> 
     '; 
    } 
    $output .= "</div>"; 
} else { 
    $output = 'No Staff Added Yet.'; 
} 

return $output; 
}; 

add_shortcode('staff', 'get_staff'); 

感謝您提供任何幫助。

回答

2

您需要將extract the attribute添加到您的查詢中。

extract(shortcode_atts(array('position' => 'doctors'), $atts)); // $position variable will be initialized to 'doctors' if the shortcode is missing the 'position' parameter 

$loop = new WP_Query(
    array (
     'post_type' => 'staff', 
     'orderby' => 'title', 
     'position' => $position 
    ) 
); 
+0

完美,謝謝。爲將來使用而書籤。 – 2013-05-06 15:42:44