我正在尝试使用分页获取标签网址。这是完整的代码。我正在尝试扩展 wp-grapphql 功能。这样我就可以获取标签分页标签网址。我有超过 3 万个标签。我能够成功获得分页类别、帖子、作者网址,但不知道为什么这get_tags()
在 wp-graphql 中无法正常工作。
//Get all tag urls for sitemap
add_action('graphql_register_types', function () {
register_graphql_field('RootQuery', 'getTagUrls', [
'type' => ['list_of' => 'String'],
'args' => [
'pageNo' => [
'type' => 'int',
],
'perPage' => [
'type' => 'int',
],
],
'description' => __('This function returns tag urls, It takes pageNo and PerPage as optional args.'),
'resolve' => function ($source, $args, $context, $info) {
$tagUrls = array();
$paged = (isset($args['pageNo'])) ? ($args['pageNo']) : 1;
$perPage = (isset($args['perPage'])) ? ($args['perPage']) : 10;
$offset = ($paged - 1) * $perPage;
$number = $perPage + $offset;
$tags = get_tags(array(
'orderby' => 'name',
'order' => 'ASC',
'number' => $number,
'offset' => $offset,
));
foreach ($tags as $tag) {
$fullUrl = esc_url(get_category_link($tag->term_id));
$url = str_replace(home_url(), '', $fullUrl);
array_push($tagUrls, $url);
}
return array_merge($tagUrls);
},
]);
});