Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active December 14, 2015 21:50
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 glueckpress/3199404 to your computer and use it in GitHub Desktop.
Save glueckpress/3199404 to your computer and use it in GitHub Desktop.
[WordPress] Call a registered sidebar in a page template dynamically based on the page slug.
<?php
// This whole thing goes into a page template,
// so we can get the page object via the_post() here
global $wp_query;
the_post();
// Store page slug for later
$gp_current_page_slug = $post->post_name;
// Page content
the_content();
// Do whatever else here...
// Reset post data for later query
wp_reset_postdata(); ?>
// Assume we have registered a widget area and based its name on the page slug
if( is_active_sidebar( "{$gp_current_page_slug}-widgets" ) )
dynamic_sidebar( "{$gp_current_page_slug}-widgets" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment