2

我有一个“员工”的自定义帖子类型。该职位类型具有“职位”分类(医生、护士等)。我正在尝试创建一个简码,允许用户在页面上仅返回特定分类的员工。

例子:

  [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'); 

谢谢你尽你所能的帮助。

4

1 回答 1

2

您需要提取属性并将其添加到您的查询中。

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
    )
);
于 2013-05-06T15:03:56.327 回答