I have a template part like this:

<nav id="subpagemenu" class="sidebar">
    <?php get_template_part( 'nav', 'subset' ); ?>
    <?php get_sidebar(); ?>

Both of these are conditional, I mean, nothing will load if there's no content. if there is content, they both load boxes like:

 <div class="widget"> 

Now I would like to be able to know if there are any widgets (sidebar, or my template_part), so that in my functions.php, I can set a variable $mywidgets=true or false.

I know that I can use '_is_active_sidebar' for the one part, but how can I check the other part? I don't want to register that part as a sidebar... All I need is a 'true/false'.

I tried 'is_page_template' and 'set_theme_mod'. I also looked at these threads:


Is there a way to check which template file is being loaded, if it is not a page template file?

Am I thinking of this in the wrong way? Need to construct the theme differently?


  • It is not clear what content you need to check for. What is in the file loaded by get_template_part?
    – s_ha_dum
    Jan 25, 2013 at 20:49
  • It's extra div.widget that I make (to show subpages or posts in category.) I used same class name for styling as real widgets. Jan 25, 2013 at 22:19

1 Answer 1


You can define a global variable in your theme functions.php

global $mywidgets;
$mywidgets = false;

in your template part add

global $mywidgets;
$mywidgets = true;

and in your functions you can check if its set to true:

function my_function(){
    global $mywidgets;
    $mywidgets = true;
    if ($mywidgets){
       // YES Your template part is loaded
       // NO Your template part is not loaded
  • If I put the a function call after declaring the variables, then it works. All this sidebar and stuff is in the footer, so I may have to get back to t his when I'm less braindead. <?php global $mywidgets; $mywidgets = true; my_function(); ?> Jan 25, 2013 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.