Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a template part like this:

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

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:

http://stackoverflow.com/questions/6366351/getting-dom-elements-by-class-name

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?

Thanks!

share|improve this question
    
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 '13 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. –  Jennifer Michelle Jan 25 '13 at 22:19
add comment

1 Answer

up vote 3 down vote accepted

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
    }else{
       // NO Your template part is not loaded
    }
}
share|improve this answer
    
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(); ?> –  Jennifer Michelle Jan 25 '13 at 22:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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