WordPress:根据元字段列出具有 A-Z 列表的作者
WordPress: List authors with A-Z listing based on meta field
我想列出所有作者并从 A-Z 排序。
问题是,我需要通过自定义元字段而不是它们的显示名称来对它们进行排序。
如果我按显示名称对作者进行排序,则一切正常。但是,如果我尝试通过自定义字段对它们进行排序,A-Z 列表将不再有效。
对于上下文:我正在使用 WooCommerce 插件 WC Vendors,作者有一个商店名称,我想将其用作订购的参考。
这是我当前的代码:
$display_admins = false;
$order_by = 'display_name'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
$role = 'vendor'; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
$avatar_size = 32;
$hide_empty = true; // hides authors with zero posts
$last = '';
if(!empty($display_admins)) {
$blogusers = get_users('orderby='.$order_by.'&role='.$role);
} else {
$admins = get_users('role=administrator');
$exclude = array();
foreach($admins as $ad) {
$exclude[] = $ad->ID;
}
$exclude = implode(',', $exclude);
$blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
}
$authors = array();
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->ID);
if(!empty($hide_empty)) {
$numposts = count_user_posts($user->ID, 'product');
if($numposts < 1) continue;
}
$authors[] = (array) $user;
}
echo '<ul class="contributors">';
foreach($authors as $author) {
//$display_name = $author['data']->display_name;
//$avatar = get_avatar($author['ID'], $avatar_size);
//$author_profile_url = get_author_posts_url($author['ID']);
$vendor_shop_name = get_user_meta( $author['ID'], 'pv_shop_name', true );
$vendor_shop_desc = get_user_meta( $author['ID'], 'pv_shop_description', true );
$vendor_shop_keyvsiual = get_user_meta( $author['ID'], 'vendor_keyvisual', true );
$vendor_shop_icon_id = get_user_meta( $author['ID'], '_wcv_store_icon_id', true );
$vendor_shop_link = WCV_Vendors::get_vendor_shop_page( $author['ID'] );
$vendor_shop_address1 = get_user_meta( $author['ID'], '_wcv_store_address1', true );
$vendor_shop_address2 = get_user_meta( $author['ID'], '_wcv_store_address2', true );
$vendor_shop_city = get_user_meta( $author['ID'], '_wcv_store_city', true );
$vendor_shop_postcode = get_user_meta( $author['ID'], '_wcv_store_postcode', true );
$vendor_shop_country = get_user_meta( $author['ID'], '_wcv_store_country', true );
// This works fine
$current = strtolower($author['data']->display_name[0]);
// This does not work
//$vendor_shop_name_start = substr($vendor_shop_name, 0, 1);
//$current = strtolower($vendor_shop_name_start);
if ($last != $current) {
echo '<a class"alphabit" name="' . strtoupper($current) . '">' . strtoupper($current) . '</a>';
$last = $current;
}
echo '<li><a href="', $vendor_shop_link, '" class="contributor-link">', $vendor_shop_name, '</a></li>';
}
echo '</ul>';
如您所见,我已经尝试将 $current
更改为元字段 pv_shop_name
。
我将字符串缩减为第一个字母。哪个工作正常,但由于某种原因,订购不再有效。例如,如果商店名称以 Ö
开头,我会看到一个特殊字符的问题。
我没有看到这里的问题。
可能跟开头的$order_by = 'display_name';
有关?
但我该如何改变呢?
我想通了。
我必须将 $oderby
更改为:
$order_by = 'meta_value';
而且我不得不将 meta_key=pv_shop_name
添加到 $blogusers
:
$blogusers = get_users('meta_key=pv_shop_name&exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
现在可以正常使用了。
我想列出所有作者并从 A-Z 排序。 问题是,我需要通过自定义元字段而不是它们的显示名称来对它们进行排序。
如果我按显示名称对作者进行排序,则一切正常。但是,如果我尝试通过自定义字段对它们进行排序,A-Z 列表将不再有效。
对于上下文:我正在使用 WooCommerce 插件 WC Vendors,作者有一个商店名称,我想将其用作订购的参考。
这是我当前的代码:
$display_admins = false;
$order_by = 'display_name'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
$role = 'vendor'; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
$avatar_size = 32;
$hide_empty = true; // hides authors with zero posts
$last = '';
if(!empty($display_admins)) {
$blogusers = get_users('orderby='.$order_by.'&role='.$role);
} else {
$admins = get_users('role=administrator');
$exclude = array();
foreach($admins as $ad) {
$exclude[] = $ad->ID;
}
$exclude = implode(',', $exclude);
$blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
}
$authors = array();
foreach ($blogusers as $bloguser) {
$user = get_userdata($bloguser->ID);
if(!empty($hide_empty)) {
$numposts = count_user_posts($user->ID, 'product');
if($numposts < 1) continue;
}
$authors[] = (array) $user;
}
echo '<ul class="contributors">';
foreach($authors as $author) {
//$display_name = $author['data']->display_name;
//$avatar = get_avatar($author['ID'], $avatar_size);
//$author_profile_url = get_author_posts_url($author['ID']);
$vendor_shop_name = get_user_meta( $author['ID'], 'pv_shop_name', true );
$vendor_shop_desc = get_user_meta( $author['ID'], 'pv_shop_description', true );
$vendor_shop_keyvsiual = get_user_meta( $author['ID'], 'vendor_keyvisual', true );
$vendor_shop_icon_id = get_user_meta( $author['ID'], '_wcv_store_icon_id', true );
$vendor_shop_link = WCV_Vendors::get_vendor_shop_page( $author['ID'] );
$vendor_shop_address1 = get_user_meta( $author['ID'], '_wcv_store_address1', true );
$vendor_shop_address2 = get_user_meta( $author['ID'], '_wcv_store_address2', true );
$vendor_shop_city = get_user_meta( $author['ID'], '_wcv_store_city', true );
$vendor_shop_postcode = get_user_meta( $author['ID'], '_wcv_store_postcode', true );
$vendor_shop_country = get_user_meta( $author['ID'], '_wcv_store_country', true );
// This works fine
$current = strtolower($author['data']->display_name[0]);
// This does not work
//$vendor_shop_name_start = substr($vendor_shop_name, 0, 1);
//$current = strtolower($vendor_shop_name_start);
if ($last != $current) {
echo '<a class"alphabit" name="' . strtoupper($current) . '">' . strtoupper($current) . '</a>';
$last = $current;
}
echo '<li><a href="', $vendor_shop_link, '" class="contributor-link">', $vendor_shop_name, '</a></li>';
}
echo '</ul>';
如您所见,我已经尝试将 $current
更改为元字段 pv_shop_name
。
我将字符串缩减为第一个字母。哪个工作正常,但由于某种原因,订购不再有效。例如,如果商店名称以 Ö
开头,我会看到一个特殊字符的问题。
我没有看到这里的问题。
可能跟开头的$order_by = 'display_name';
有关?
但我该如何改变呢?
我想通了。
我必须将 $oderby
更改为:
$order_by = 'meta_value';
而且我不得不将 meta_key=pv_shop_name
添加到 $blogusers
:
$blogusers = get_users('meta_key=pv_shop_name&exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
现在可以正常使用了。