Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Hooks - Actions and Filters Filter Functions in WordPress Real World Examples of WordPress Filters

Carl Sergile
Carl Sergile
16,570 Points

Add filter to a div

Hey, Just like the body_class(), is there a way to add a filter to a specific div? I have this conditional function running in my function.php that I am trying to output only a certain div instead of the whole body of the page. Is that possible?

The thing is, the conditional does work, but when I apply it, it gets applied to the whole body of the page, which affects my jumbotron. I just want the conditional function to just run on one specific div.

Let me know if I need to be more clear or provide any additional code.

P.S. Using front-page.php if that makes any difference.

Could you share a sample of the code for this? I'm not sure of a way to target specific HTML elements in PHP but what you could do is write a function that outputs this div, and then write a filter for that function.

Carl Sergile
Carl Sergile
16,570 Points

so pretty much trying to create a front-page widget grid system, by outputting different classes based on the number of widgets on a page. Let me know if you want me to clarify this further as it is super confusing to get, even for me at times.

1 Answer

Carl Sergile
Carl Sergile
16,570 Points

<?php

// All your classes and column logic is here

// Check if only single sidebar if ( is_active_sidebar( 'sidebar-1' ) || is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-3' ) ) {

$container_class = 'col-md-12';
$sidebar_total_class = 'col-md-12';
$sidebar_class = 'col-md-12';

// Check which sidebar
if ( is_active_sidebar( 'sidebar-1' ) ) {

    $sidebar = 'sidebar-1';
}
else if ( is_active_sidebar( 'sidebar-2' ) ) {

    $sidebar = 'sidebar-2';
}
else if ( is_active_sidebar( 'sidebar-3' ) ) {

    $sidebar = 'sidebar-3';
}

}

// Check if two sidebars else if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-3' ) || is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) ) {

$container_class = 'col-md-6';
$sidebar_total_class = 'col-md-6';
$sidebar_class = 'col-md-6';

// Check which sidebar
if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' )) {

    $sidebar = ['sidebar-1', 'sidebar-2'];
}
else if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-3' )) {

    $sidebar = ['sidebar-1', 'sidebar-3'];
}
else if ( is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' )) {

    $sidebar = ['sidebar-2', 'sidebar-3'];
}

} // Check if all three sidebars else if ( is_active_sidebar( 'sidebar-1' ) && is_active_sidebar( 'sidebar-2' ) && is_active_sidebar( 'sidebar-3' ) ) {

$container_class = 'col-md-4';
$sidebar_total_class = 'col-md-8';
$sidebar_class = 'col-md-4';
$sidebar = ['sidebar-1', 'sidebar-2', 'sidebar-3'];

} // No sidebar else {

$container_class = col-md-12;

}

// Wrapper to keep things on same row + column management ?> <div class="container col-md-12"> <div class="row"> <?php

    // Content container + column management ?>

    <div class="container <?php echo $container_class; ?>"> </div> <?php



    // Sidebar

    // Check if any sidebars
    if ( ! empty( $sidebar ) ) {

        // Wrapper to keep things on row + column management ?>
        <div class="container <?php echo $sidebar_total_class; ?>">
            <div class="row"> <?php

                // Check if multiple
                if( is_array( $sidebar ) ) {

                    // Loop through each
                    foreach ( $sidebar as $single_sidebar ) { ?>

                        <div class="<?php echo $sidebar_class; ?>"> <?php

                            dynamic_sidebar( $single_sidebar ); ?>

                        </div> <?php
                    }
                }
                // Not multiple, only one
                else { ?>

                    <div class="<?php echo $sidebar_class; ?>"> <?php

                        dynamic_sidebar( $sidebar ); ?>

                    </div> <?php
                } ?>

            </div>
        </div> <?php
    } ?>
</div>

</div>