The get_current_blog_id() WordPress PHP function retrieves the current site ID.
Usage
$blog_id = get_current_blog_id();
Parameters
- None
More information
See WordPress Developer Resources: get_current_blog_id()
Examples
Displaying the current blog ID
This example retrieves the current blog ID and displays it on the page.
// Get the current blog ID $current_blog_id = get_current_blog_id(); // Display the blog ID echo "The current blog ID is: " . $current_blog_id;
Conditional content based on blog ID
This example shows or hides content based on the current blog ID.
// Get the current blog ID $current_blog_id = get_current_blog_id(); // Show content only for blog ID 3 if ($current_blog_id == 3) { echo "This content is visible only for blog ID 3."; }
Displaying the current blog’s name
This example retrieves the current blog’s name using the blog ID.
// Get the current blog ID $current_blog_id = get_current_blog_id(); // Get the current blog's name $current_blog_name = get_bloginfo('name', $current_blog_id); // Display the blog name echo "The current blog's name is: " . $current_blog_name;
Getting the current blog’s posts
This example retrieves the 5 most recent posts from the current blog.
// Get the current blog ID $current_blog_id = get_current_blog_id(); // Get the 5 most recent posts from the current blog $args = array( 'numberposts' => 5, 'orderby' => 'post_date', 'order' => 'DESC', 'post_status' => 'publish', 'blog_id' => $current_blog_id, ); $recent_posts = wp_get_recent_posts($args); // Display the recent posts foreach ($recent_posts as $post) { echo '<h2>' . $post['post_title'] . '</h2>'; }
Switching to a specific blog using the blog ID
This example switches to a specific blog, retrieves its name, and then restores the current blog.
// Get the current blog ID $current_blog_id = get_current_blog_id(); // Switch to a specific blog (e.g. blog ID 2) switch_to_blog(2); // Get the blog's name $switched_blog_name = get_bloginfo('name'); // Display the switched blog's name echo "Switched to blog: " . $switched_blog_name; // Restore the current blog switch_to_blog($current_blog_id);