
I have been trying to create a list of subsite links on my main site using an ACF options page value get_field( 'nn_city_name', 'option' );
as the text for each link. I can get a working list, but my attempts to sort the list by nn_city_name
all result in errors.
Here’s my function:
function x_list_sites() {
$subsites = get_sites(
array(
'site__not_in' => '1',
// 'orderby' => 'domain',
)
);
if ( !empty( $subsites ) ) {
echo '<h3>Nerd Nite Cities</h3>';
echo '<ul class="subsites">';
foreach ( $subsites as $subsite ) {
$subsite_id = get_object_vars( $subsite )[ 'blog_id' ];
switch_to_blog( $subsite_id );
$subsite_name = get_field( 'nn_city_name', 'option' );
$subsite_link = get_blog_details( $subsite_id )->siteurl;
echo '<li class="site-' . $subsite_id . '"><a href="' . $subsite_link . '">' . $subsite_name . '</a></li>';
}
restore_current_blog();
echo '</ul>';
}
}
Here’s a test page that puts the list in the sidebar: https://nnalphastaging.wpengine.com/testing-1-2-3/
Notes:
- I feel like I should be able to do some kind of array_push() to add
nn_city_name
to $subsites
so that I can sort by nn_city_name
, but my attempts have failed.
'orderby' => ‘domain’
gets most of the subsites into correct order, but you run into problems with a subdomain like “nyc” for “New York”.
Thanks in advance!