我有以下自定义短代码,在定义的“类型”分类中显示自定义职位类型的职员。我正在尝试添加第二个属性,允许用户定义输出的布局样式,例如列或行。以下是我设想的短代码:
[staff type="international" style="rows"]
我认为我拥有获取属性的所有代码,包括设置默认值,但我似乎无法弄清楚如何在那里获得if语句来改变输出。看起来它应该在第一个“$ output。=”行之前。
function get_staff($atts) {
extract( shortcode_atts( array(
'type' => 'international',
'style' => 'rows'
), $atts ) );
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$loop = new WP_Query(
array (
'post_type' => 'staff',
'orderby' => 'title',
'staff-type' => $type,
'style' => $style
)
);
remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );
if ($loop->have_posts()) {
$output = '<div class="staff">';
while($loop->have_posts()){
$loop->the_post();
$meta = get_post_meta(get_the_id());
// Attributes Array for Featured Image Below
$attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => 'img_frame'
);
$output .= '
<div class="row-fluid" style="border-bottom: 1px solid #EEE; margin-bottom: 16px; padding-bottom: 16px;">
<div class="span3">' . get_the_post_thumbnail($post->ID, 'small', $attr) . '</div>
<div class="span9"><h3>' . get_the_title() . '</h3>
' . get_the_content() . '
</div></div>
';
}
$output .= "</div>";
} else {
$output = 'No Staff Meet This Criteria Yet.';
}
return $output;
};
// Create Last Name Sort For Staff Custom Post Type
function posts_orderby_lastname ($orderby_statement)
{
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
return $orderby_statement;
}
add_shortcode('staff', 'get_staff');
答案 0 :(得分:1)
根据您的编码风格,有几种方法。您可以在开始循环帖子之前检查style
param 。另一种方法是在开始循环后执行此操作。当然,有一些代码重复,但它更容易阅读,你将拥有你想要生成的确切标记所需的所有控制。
$output = '<div class="staff">';
if ($loop->have_posts()) {
if($style == 'rows'){ // rows
$output .= '<div class="layout-rows">';
while( $loop->have_posts() ){
// ...
}
$output .= "</div>";
} elseif($style == 'columns') { // columns
$output .= '<div class="layout-columns">';
while( $loop->have_posts() ){
// ...
}
$output .= "</div>";
} else { // everything else
$output .= '<div class="layout-default">';
while( $loop->have_posts() ){
// ...
}
$output .= "</div>";
}
} else {
$output = 'No Staff Meet This Criteria Yet.';
}
$output .= "</div>";