Ric*_*Coy 8 wordpress alphabetical custom-post-type shortcode
我有一个自定义帖子类型的"工作人员".我需要通过页面上的姓氏按字母顺序显示工作人员.我知道一个解决方法是使用自定义元框并将名字和姓氏分成两个字段,但我试图避免这样做,因为它看起来非常hackish并且不像使用title字段那样干净.
我有一个短代码工作,将显示自定义的帖子类型与请求的工作人员"类型"分类属性.这是一个例子:
[staff type="local"]
Run Code Online (Sandbox Code Playgroud)
我不能只是假设这是我想要的标题中的第二个单词,因为一些工作人员是夫妻,并且他们的名字将被列为:"Bob and Cindy Smith".
这是我到目前为止的短代码.
function get_staff($atts) {
extract( shortcode_atts( array( 'type' => 'international' ), $atts ) );
$loop = new WP_Query(
array (
'post_type' => 'staff',
'orderby' => 'title',
'staff-type' => $type
)
);
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 Meet This Criteria Yet.';
}
return $output;
};
add_shortcode('staff', 'get_staff');
Run Code Online (Sandbox Code Playgroud)
这很好用,但缺少按姓氏按字母顺序排列.谢谢你尽你所能的帮助.这是我第一次尝试精心设计的短代码,所以请回答一下.
use*_*010 13
试试这个.首先在functions.php中添加以下orderby过滤器
function posts_orderby_lastname ($orderby_statement)
{
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) DESC";
return $orderby_statement;
}
Run Code Online (Sandbox Code Playgroud)
然后在你的查询中使用它
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$loop = new WP_Query(
array (
'post_type' => 'staff',
'staff-type' => $type
)
);
Run Code Online (Sandbox Code Playgroud)
并在循环后删除过滤器
remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );
Run Code Online (Sandbox Code Playgroud)