Skip to content

Instantly share code, notes, and snippets.

@mustardBees
Last active December 16, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustardBees/5433628 to your computer and use it in GitHub Desktop.
Save mustardBees/5433628 to your computer and use it in GitHub Desktop.
WordPress, get a sidebar as displayed on another page
<?php
/**
* Get sidebar as displayed on another page
*
* A dirty way to grab a sidebar as it's displayed on another page. Useful for
* when widgets output a menu and you need to display the same menu on a post
* type archive/single page.
*
* @author Phil Wylie
* @link http://goo.gl/qYf8H
* @param string $dynamic_sidebar Name or ID of dynamic sidebar
* @param int $page_id The page ID to get the sidebar from
*/
function iweb_get_dynamic_sidebar( $dynamic_sidebar, $page_id = null ) {
ob_start();
if ( ! is_null( $page_id ) ) {
global $post;
$post = get_post( $page_id );
dynamic_sidebar( $dynamic_sidebar );
wp_reset_postdata();
} else {
dynamic_sidebar( $dynamic_sidebar );
}
$output = ob_get_clean();
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment