我试图在我的提要上显示帖子编号(用于排名),这是通过调用包含wordpress循环的函数并返回递增的值来实现的。但是,通过这样做,循环似乎停止查找新帖子,而是重复内容(仅重复此函数调用下面的内容),例如文章标题(特征图像不同,因为它是在函数之前调用的)。我该如何解决这个问题?
下面是它的样子:
在中找到循环
plugin.php
function ori_load_more_func() {
//removed irrelevant code
// Query the posts by the specified IDs, and sort by like count
$args = array(
\'post_type\' => array( \'post\'),
\'post__in\' => $most_popular_posts,
\'meta_key\' => \'_liked\',
\'orderby\' => array( \'meta_value_num\' => \'DESC\', \'date\' => \'DESC\' ),
\'post_status\' => \'publish\',
\'paged\' => $offset,
\'posts_per_page\' => $posts_per_page,
\'post__not_in\' => array($current_post_id) //skip current displayed post
);
ob_start();
global $ranking;
$posts_query = new WP_Query( $args );
if ($posts_query->have_posts()) {
$result[\'have_posts\'] = true;
// echo \'<div class="load_more_single_post_container">\';
while ( $posts_query->have_posts() ) : $posts_query->the_post();
return $ranking += 1;
echo get_template_part( \'template-parts/content\', get_post_format() );
endwhile;
// $ranking = $ranking + 1;
//echo \'</div>\';
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
}
调用函数的位置
content.php
<div class="rank-number">
<?php echo ori_load_more_func($ranking); ?>
</div>