WordPress Orderby标题中的最后一个单词 [英] WordPress Orderby Last Word In Title

查看:96
本文介绍了WordPress Orderby标题中的最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义帖子类型为工作人员。我需要获取此信息以按页面上姓氏的字母顺序显示人员。我知道一种解决方法是使用自定义元框,并将名字和姓氏分成两个字段,但是我试图避免这种情况,因为这看起来很hacky,而且不像使用title字段那样干净。

I have a custom post type of "staff". I need to get this to display the staff alphabetically by last name on the page. I know a work around would be to use custom meta boxes and break up first and last names into two fields but I'm trying to avoid that as that seems very hackish and not as clean as just using the title field.

我有一个简短的代码工作,它将显示带有请求的人员类型分类法属性的自定义帖子类型。这是一个示例:

I have a shortcode working that will show the custom post type with the staff "type" taxonomy attribute requested. Here is an example:

[staff type="local"] 

我不能仅仅假设这是我想要的标题中的第二个单词,因为一些职员是夫妻,并且他们的名字都列出了,例如:和辛迪·史密斯。

I can't just assume that it's the second word in the title that I want since some staff are couples and will have both of their names listed like: "Bob and Cindy Smith".

这是我到目前为止的简码。

Here is the shortcode that I have so far.

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

这很好用,但是缺少按字母顺序排列的姓氏。谢谢你尽你所能的帮助。这是我第一次尝试编写详细的简短代码,因此请对您的答案进行一些具体说明。

This works great but is missing the alphabetizing by last name. Thanks for any help you can offer. This is my first real attempt at an elaborate shortcode so please be a little specific with your answer.

推荐答案

尝试一下。首先在functions.php中添加以下 orderby过滤器

Try this. First add the following orderby filter in functions.php

function posts_orderby_lastname ($orderby_statement) 
{
  $orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) DESC";
    return $orderby_statement;
}

然后像这样在查询中使用它

and then use it in your query like so

add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
    $loop = new WP_Query(
        array (
            'post_type' => 'staff',
            'staff-type' => $type
        )
    );

并在循环后删除过滤器

remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );

这篇关于WordPress Orderby标题中的最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆