George Mason University Antonin Scalia Law School

List All Sites Using Custom Shortcode

Shortcodes make it easier to customize wordpress pages quickly. WordPress comes with pre-built shortcodes but we’re going to build one from scratch. You will need to add a function to your child theme’s functions.php file. This particular code builds an ordered list of all our wordpress sites under our sls.gmu.edu domain using the get_sites() function. This function will work on all WP platforms 4.3 and above.


/*
* Author: Matthew
* This function creates a shortcode usable in all sites using the scalia law theme to create an ordered list of
all multisites
*/
function scalia_list_sites() {

$subsites = get_sites(); //creates variable using get_sites

if ( ! empty ( $subsites ) ) {

$html = '

    '; //sets up class

    foreach( $subsites as $subsite ) {

    $subsite_id = $subsite->blog_id; //finds sites by id
    $subsite_name = get_blog_details( $subsite_id )->blogname; //creates variable for site name
    $subsite_link = get_blog_details( $subsite_id )->siteurl; //creates variable for site url
    $html .= '< li class="site-' . $subsite_id . '">< a href="' . $subsite_link . '">' . $subsite_name . '< /a >< /li >'; //builds individual list items
    sort($html);
    }

    $html .= '< /ol >';

    return $html; //returns the list

    }

    }
    add_shortcode('scalia_list', 'scalia_list_sites'); //calls function and sets up [ scalia_list ] custom short code

    This goes in your child-theme’s functions.php file.