Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.