夏期劇場
夏期劇場

Reputation: 18337

Wordpress : How to programmatically GET (to know) which Sidebar (or) which Menu is being used on Different Pages?

Lets say i want to detect such thing from the "functions.php" .. to know which Sidebar (or) which Menu is being loaded on current (whatever landed) page, how can i know?

Lets say i have a few pages, with:

From functions.php, if:

So the thing here is:

How to simply do it please.

Upvotes: 0

Views: 183

Answers (2)

ViszinisA
ViszinisA

Reputation: 462

You can hook action and filter for specific functions.

add_action( 'get_sidebar', 'get_sidebar_name' );
function get_sidebar_name($name) {
    var_dump($name);
}

Variable $name will contain name for sidebar. If empty then it will be default sidebar.

add_filter( 'wp_nav_menu_args', 'random_wp_nav_menu_args' );
function random_wp_nav_menu_args( $args = '' ) {
    print_r($args);
    return $args;
}

$args variable contains almost the same data you pass to function. You can see theme_locationa and menu name. For wp_nav_menu there are other filter you can hook into.

You can see action call in get_sidebar function source

And in wp_nav_menu function source

Upvotes: 1

gaurav kumar
gaurav kumar

Reputation: 1091

<?php 
$pageid = get_the_ID(); //to get page id

if( $pageid == 12 ) //here 12 apples page id
{
  echo "Condition C";
}
if( $pageid == 13 ) //here 13 orange page id
{
  echo "Condition C";

//for sidebar

if( dynamic_sidebar('To-do List')) 
{
 echo "Condition A";
}
if( dynamic_sidebar('Today Weather')) 
{
 echo "Condition B";
}
or,

<?php 
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) { 

         echo $sidebar['id']; 
         echo $sidebar['name'];

 }
 ?>

Upvotes: 0

Related Questions