How to customize a section of a widget only on certain pages or page

I am using the twentynineteen default Wordpress theme but I would like to know how I can remove this line ?php get_template_part( 'template-parts/footer/footer', 'widgets' ); ? only on certain pages in footer.php. Similar to this post: How to add a specific widget to only 1 page?. Basically, I would like to know how I can change a sections of a widget in a child theme on certain pages.

Topic footer child-theme widgets themes Wordpress

Category Web


I don't think there's a simple way to force WordPress to exclude a template part, or load a different one. See 1 and 2 for some Stack Exchange discussion and workarounds. BUT in the special case of widgets, you can implement a filter (namely,widget_display_callback) in which you check where the widget is to be displayed, and what kind of page it might be displayed on, and tell WordPress not to display it (by returning FALSE). See more from Stack Exchange 3 and the WordPress Code Reference 4.

As far as workarounds go, probably the simplest is to use a CSS display: none to have the browser not display the widgets (or perhaps the entire widget area) on certain pages (if need be, there are simple ways to augment the CSS classes for the body tag to indicate what kind of page it is).

Edit: addition

Given that you'll want to make a child theme, and the Twentynineteen theme is well-organized, I thought of a couple of alternatives, which should be pretty simple.
child theme with special template part
in your child theme, make a template-parts directory, make a footer directory inside it, then copy template-parts/footer/footer-widgets.php from the Twentynineteen theme into the corresponding place in your child theme. Modify the copy to add your condition; something like this

if ( is_active_sidebar( 'sidebar-1' ) ) : ?>

    <aside class="widget-area" role="complementary" aria-label="<?php esc_attr_e( 'Footer', 'twentynineteen' ); ?>">
        <?php
            $cats = get_the_category();
            $cat_name = $cats[0]->name;
            print_r($cat_name);

            if ( is_active_sidebar( 'sidebar-1' ) and
                (strcasecmp('category with no footer widgets', $cat_name) == 0)) {
                ?>
                    <div class="widget-column footer-widget-1">
                        <?php dynamic_sidebar( 'sidebar-1' ); ?>
                    </div>
                <?php
            }
        ?>
    </aside><!-- .widget-area -->

<?php endif; ?>
  • sidebars_widgets filter
    Use the sidebars_widgets filter to remove the widget area entirely. something like add_filter( 'sidebars_widgets', 'remove_footer_widget_area' );

    function remove_footer_widget_area( $sidebars_widgets ) {
                    $cats = get_the_category();
                    $cat_name = $cats[0]->name;
                    if( (strcasecmp('category with no footer widgets', $cat_name) == 0) ) { 
                        unset($sidebars_widgets['sidebar-1']);
                    }
                    return $sidebars_widgets;
                }
    

    see 5

a few notes:
— I'm assuming that the test as to whether to display the widgets depends on the category of the page, and the page is only in one category. Modify as needed.
— For historical reasons, 'sidebars' here really means 'widget areas'. WordPress is full of anachronisms like this.
— Another anachronism is the function name get_the_category(), which gets an array of categories. So it really should be called get_the_categories().
— The second approach copies-and-modifies a template file from the parent theme. But the template is small and unlikely to change.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.