正如您所述,排名是由自定义字段中的值定义的,因此您只需要从自定义字段中获取值并显示它。
您可以在循环中尝试以下操作
global $post;
// Just make sure the custom field name is correct
$rank = get_post_meta( $post->ID, \'custom_field\', true );
// Display the title and rank
echo \'Rank \' . $rank . \' \' . get_the_title();
编辑代码中的顺序不正确。循环一应根据custum字段值进行排序。在循环2中,您需要按照传递给的ID的顺序进行排序
post_in
您的完整代码应该如下所示
$args = [
\'posts_per_page\' => 100,
\'fields\' => \'ids\',
\'meta_key\' => \'custom_field\',
\'orderby\' => \'meta_value_num\', //or \'meta_value_num\'
\'order\' => \'DESC\',
// Add additional args here
];
$post_ids = get_posts( $args );
if ( $post_ids ) {
$paged = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
$args_2 = [
\'paged\' => $paged,
\'post__in\' => $post_ids,
\'posts_per_page\' => 10,
\'orderby\' => \'post__in\',
];
$q = new WP_Query( $args_2 );
while ( $q->have_posts() ) {
$q->the_post();
global $post;
// Just make sure the custom field name is correct
$rank = get_post_meta( $post->ID, \'custom_field\', true );
// Display the title and rank
echo \'Rank \' . $rank . \' \' . get_the_title();
// Rest of your loop here
}
next_posts_link( \'Next Posts\', $q->max_num_pages );
previous_posts_link( \'Previous Posts\' );
wp_reset_postdata();
}
编辑2要在查询中将排名显示为数字,可以执行以下操作:;(
NOTE: 这是未经测试的
function get_post_rank( $query = \'\' )
{
global $wp_query;
// If $query is empty or not an instanceof WP_Query, use $wp_query
if ( !$query || !$query instanceof WP_Query)
$query = $wp_query;
// Set up our variables for calculations
$current_post = ( $query->current_post + 1 );
$paged = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
$ppp = $query->query_vars[\'posts_per_page\'];
// If this is the first page, simple return the current post number
if ( $paged == 1 )
return $current_post;
// If current page is not 1, calculate our post number
return ( $paged * $ppp ) - ( $ppp - $current_post );
}
然后在循环内执行以下操作:
$args = [
\'posts_per_page\' => 100,
\'fields\' => \'ids\',
\'meta_key\' => \'custom_field\',
\'orderby\' => \'meta_value_num\', //or \'meta_value_num\'
\'order\' => \'DESC\',
// Add additional args here
];
$post_ids = get_posts( $args );
if ( $post_ids ) {
$paged = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
$args_2 = [
\'paged\' => $paged,
\'post__in\' => $post_ids,
\'posts_per_page\' => 10,
\'orderby\' => \'post__in\',
];
$q = new WP_Query( $args_2 );
while ( $q->have_posts() ) {
$q->the_post();
$rank = get_post_rank( $q );
// Display the title and rank
echo \'Rank \' . $rank . \' \' . get_the_title();
// Rest of your loop here
}
next_posts_link( \'Next Posts\', $q->max_num_pages );
previous_posts_link( \'Previous Posts\' );
wp_reset_postdata();
}